javascript - 无法使用带有 angular2 和 angular-cli 的 Electron 定位 vendor 文件

标签 javascript angular electron angular-cli

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

npm start 启动我的应用程序,按预期工作。

但是,在 electron 中启动应用程序似乎失败得很惨。对于我的生活似乎无法弄清楚为什么 Electron 无法找到 dist/ 中包含的文件夹。

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

pretty standard...

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

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://协议(protocol),您需要执行以下操作:

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

来自:

<base href="/">

<base href="./">

希望这对您有所帮助。

编辑:此外,忘记提及:如果您同时在线和离线运行您的应用程序,您可能想要使用某种检测,以便您可以在应用程序在正常运行时切换回/基地浏览器窗口,通过 HTTP。

关于javascript - 无法使用带有 angular2 和 angular-cli 的 Electron 定位 vendor 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622389/

相关文章:

angular - list : Line: 1, 列 : 1, 语法错误。 AWS 错误

angular - Playright 使用 global-setup.ts 缓存 session 超时登录到应用程序,等待 DEBUG=0 的选择器,而 DEBUG=1 则不会超时

javascript - 从div中的另一个页面访问html元素

javascript - 如何在 Electron 中正确使用preloads和window.api调用?

javascript - showMessageBox promise 永远不会被调用

javascript - JS - 检测 uBlock 来源

javascript - 过渡未正确执行

javascript - 使用BeautifulSoup获取 "View Element"代码而不是 "View Source"代码

模板文字字符串问题中的 Javascript 函数参数

Angular 4 更新数组推送 View