Angular 应用在 Electron 中 运行 时中断
Angular apps break when run in Electron
我开始构建一个 Electron 应用程序,当我尝试在其中 运行 一个 angular 分发时遇到了一些问题。
对于 angular 应用程序,我使用 yeoman 和 g运行t 进行构建。如果我使用 grunt serve
开发 angular 应用程序并在 localhost:9000 上使用 运行 开发它,Electron 应用程序可以很好地选择应用程序。但是,如果我 运行 g运行t build 并将 Electron 应用程序指向静态文件,我会收到一些 angular 错误。
[$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.
在 electron 应用程序中,我 运行 使用 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;
});
/* ----------------------- */
});
通过用 nodeIntegration: false
初始化电子的 BrowserWindow
解决了这个问题
mainWindow = new BrowserWindow({width: 1024, height: 764, title: "app", webPreferences: {"nodeIntegration":false}});
你的回答是正确的。如果您想维护节点集成,您还可以在 index.html
中使用此代码,然后再加载任何其他脚本:
<script>
window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;
</script>
我开始构建一个 Electron 应用程序,当我尝试在其中 运行 一个 angular 分发时遇到了一些问题。
对于 angular 应用程序,我使用 yeoman 和 g运行t 进行构建。如果我使用 grunt serve
开发 angular 应用程序并在 localhost:9000 上使用 运行 开发它,Electron 应用程序可以很好地选择应用程序。但是,如果我 运行 g运行t build 并将 Electron 应用程序指向静态文件,我会收到一些 angular 错误。
[$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.
在 electron 应用程序中,我 运行 使用 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;
});
/* ----------------------- */
});
通过用 nodeIntegration: false
BrowserWindow
解决了这个问题
mainWindow = new BrowserWindow({width: 1024, height: 764, title: "app", webPreferences: {"nodeIntegration":false}});
你的回答是正确的。如果您想维护节点集成,您还可以在 index.html
中使用此代码,然后再加载任何其他脚本:
<script>
window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;
</script>