无法使用带有 angular2 和 angular-cli 的电子定位供应商文件

Unable to locate vendor files using electron with angular2 and angular-cli

我使用 angular-cli 创建了一个示例英雄应用程序(快速入门中的应用程序),它按预期工作。

npm start 开发了我的应用程序,按预期工作。

然而,在 electron 中启动应用程序似乎失败得很惨。对于我的生活似乎无法弄清楚为什么电子无法找到 dist/.

中包含的文件夹

即我的 dist/ 目录有以下内容:

但是,当我启动 electron dist/(其中包含 index.js 电子主文件)时,控制台输出以下内容:

Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/systemjs/dist/system.src.js Failed to load resource: 

但显然它们在 vendor/ 目录中,因为我的 npm start 工作正常。

我的 index.js 几乎是入门的副本:

const electron = require('electron');
// Module to control application life.
const {app} = electron;
// Module to create native browser window.
const {BrowserWindow} = electron;

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600});

  // and load the index.html of the app.
  win.loadURL(`file://${__dirname}/index.html`);


  // Emitted when the window is closed.
  win.on('closed', () => {
    // 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.
    win = null;
  });
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

为了让您的应用程序使用 file:// 协议,您需要执行以下操作:

在src/index.html里面,将下面这行改成:

发件人:

<base href="/">

<base href="./">

希望对您有所帮助。

编辑:此外,忘记提及:如果您的应用程序同时处于在线和离线状态 运行,您可能希望使用某种检测,以便在应用程序 运行 在普通浏览器 window 中,通过 HTTP。

使用最新的 angular-cli 版本,您需要将基本 href 路径作为参数传递给 ng build 命令,而不是在 index.html 文件中更改它:

ng build --base-href .

希望对您有所帮助。