【问题标题】:Node's and Electron's modules from inside Angular2Angular2 内部的 Node 和 Electron 模块
【发布时间】:2017-06-08 02:59:06
【问题描述】:

我正在使用 Angular2 开发一个 Electron 应用程序。

在电子的main.js 我正在引用/加载 NG App:

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

let win

function createWindow () {
  win = new BrowserWindow({width: 800, height: 600})

  // load the index.html of the NG app:
  win.loadURL(url.format({
    pathname: path.join(__dirname, '/../../dist/index.html'),
    protocol: 'file:',
    slashes: true
  }))

[...]

这就像一个魅力。但是,我现在想从 NG 部分内部访问 node 和 electron 的模块。

当我尝试导入例如:fs 模块时:

import * as fs from "fs";

它仍然可以编译,但每当我调用 fs.readFile(...) 时,它都会说:

__WEBPACK_IMPORTED_MODULE_2_fs__.readFile is not a function

当我想到它时,这不起作用也不能起作用,因为模块不在node_modules 文件夹中(对吗?)。 我需要做什么才能使它们在 NG 部分中可用?

【问题讨论】:

    标签: node.js angular typescript electron


    【解决方案1】:

    如果这仍然相关 -

    我还不知道这种“官方”方式。但是有一些变通的解决方案 - 主要是在 index.html 中需要电子/其他模块并访问window['electron'] -

    <script>
        window.electron = require('electron');
    </script>
    

    或创建访问电子对象的角度服务。

    declare const window: ElectronWindow;
    export class ChildProcessService {...}
    

    你可以看到这个Here的实现

    • ElectronWindow 指的是一个自定义接口,您可以创建并添加一个 require() 函数来处理类型。
    • 使用window.require(*some-node-module*) 存储模块

    【讨论】:

      【解决方案2】:

      您不能直接从 Angular 内部调用 Electron / Node 模块。相反,请查看the Electron remote API

      【讨论】:

      • 不幸的是,我什至无法访问electron 模块来加载remote from:import {remote} from "electron"; throws:Cannot find name 'electron'
      猜你喜欢
      • 2017-03-20
      • 2016-05-23
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2021-03-18
      相关资源
      最近更新 更多