【问题标题】:Angular apps break when run in ElectronAngular 应用程序在 Electron 中运行时中断
【发布时间】:2016-03-23 21:56:55
【问题描述】:

我开始构建一个 Electron 应用程序,当我尝试在其中运行角度分布时遇到了一些问题。

对于 Angular 应用程序,我使用 yeoman 和 grunt 进行构建。如果我使用 grunt serve 开发 Angular 应用程序并在 localhost:9000 上运行它,Electron 应用程序可以很好地选择应用程序。但是,如果我运行 grunt build 并将 Electron 应用程序指向静态文件,则会出现一些角度错误。

[$injector:modulerr] Failed to instantiate module clientApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngResource due to:
Error: [$injector:nomod] Module 'ngResource' is not available! You either
misspelled the module name or forgot to load it. If registering a module 
ensure that you specify the dependencies as the second argument.

在电子应用程序中,我使用 express 运行服务器,并且我还尝试在 localhost:8000 选择通过 express 运行 angular 应用程序。那也没有用。我还尝试运行另一个部署在我的服务器上并在浏览器中正常工作的 Angular 应用程序 - 在 Electron 中不起作用。所有尝试都出现相同的错误。

我还必须提到,在所有情况下,如果我在浏览器中打开 Angular 应用程序,它就可以正常工作。

这是电子密码:

app.on('ready', function() {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 1024, height: 764, title: "uMaster"});

  // and load the index.html of the app.
  var url = path.join(__dirname, "dist", "index.html");
  url = "file://" + url;
  console.log(url);

  // mainWindow.loadURL(url); ---- this is not working in Electron
  // mainWindow.loadURL("http://localhost:8000"); -- not working when served by express
  mainwindow.loadURL("http://localhost:9000"); // working when angular runs with grunt serve

  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });

  /* ----------------------- */
});

【问题讨论】:

  • 听起来你错过了对angular-resource.min.js的引用
  • 不是这样,如果修改了angular模块的顺序,只会说第一个模块没有被引用,它会崩溃。参考显然在那里,因为它在电子之外运行。

标签: angularjs node.js electron


【解决方案1】:

通过使用nodeIntegration: false 初始化电子的BrowserWindow 解决了这个问题

mainWindow = new BrowserWindow({width: 1024, height: 764, title: "app", webPreferences: {"nodeIntegration":false}});

【讨论】:

    【解决方案2】:

    你的答案是正确的。如果您想维护节点集成,您也可以在您的index.html 中使用此代码,然后再加载任何其他脚本:

    <script>
      window.nodeRequire = require;
      delete window.require;
      delete window.exports;
      delete window.module;
    </script>
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2021-02-09
      • 2021-01-17
      • 2018-09-07
      相关资源
      最近更新 更多