2

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

npm start提出我的应用程序,按预期工作。

但是,在电子中启动应用程序似乎失败了。对于我的生活,我似乎无法弄清楚为什么电子无法找到其中包含的文件夹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.
4

2 回答 2

8

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

在 src/index.html 中,将以下行更改如下:

从:

<base href="/">

<base href="./">

希望这可以帮助。

编辑:另外,忘了提及:如果您在线和离线运行您的应用程序,您可能需要使用某种检测,以便当应用程序在普通浏览器窗口中运行时,您可以切换回 / base,通过 HTTP。

于 2016-06-06T17:25:44.207 回答
1

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

ng build --base-href .

希望能帮助到你。

于 2017-02-12T18:50:50.240 回答