无法使用带有angular2和angular-cli的电子来查找供应商文件

时间:2016-06-03 19:54:28

标签: javascript angular electron angular-cli

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

npm start提供了我的应用,按预期工作。

然而,在电子中启动应用程序似乎失败了。因为我的生活似乎无法弄清楚为什么电子无法找到dist/中包含的文件夹。

即。我的dist/目录包含以下内容:

pretty standard...

但是,当我启动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.

2 个答案:

答案 0 :(得分:7)

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

在src / index.html内部,更改以下行:

自:

<base href="/">

<base href="./">

希望这有帮助。

编辑:另外,忘了提一下:如果您在线和离线运行应用程序,您可能需要使用某种检测,这样当应用程序在正常情况下运行时您可以切换回/ base浏览器窗口,通过HTTP。

答案 1 :(得分:1)

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

ng build --base-href .

希望它有所帮助。