如何使用Angular在Electron的新窗口中打开component.html?

时间:2019-08-07 22:14:10

标签: angular electron

我正在使用Angular 7建立一个用Electron创建的简单项目(使用该项目https://github.com/maximegris/angular-electron)。

如何在新窗口中打开component.html?

我已经做到了:

    createBrowserWindow() {
    const remote = this.electronService.remote;
    const BrowserWindow = this.electronService.remote.BrowserWindow;
    var win = new BrowserWindow({
     height: 600,
     width: 800
     });



    win.loadURL(this.electronService.url.format({
      pathname: this.electronService.path.join(__dirname, './index.html'),
      protocol: 'file:',
      slashes: true,
      hash: '/inicio'
    }) 
    ); 

    win.webContents.openDevTools()

    win.on('closed', () => {
      win = null
    });
  }

但是当我调用该方法时,会出现错误:

  

zone.js:703未处理的承诺拒绝:ERR_FILE_NOT_FOUND(-6)加载'file:/// C:/Users/xx/Documents/projectName/node_modules/electron/dist/resources/electron.asar/renderer/index .html#/ inicio';

然后将打开一个新窗口,并显示以下消息:

  

不允许加载本地资源:file:/// C:/Users/xx/Documents/projecName/node_modules/electron/dist/resources/electron.asar/renderer/index.html#/inicio

1 个答案:

答案 0 :(得分:0)

尝试这样使用:

import { app, BrowserWindow } from "electron";
import * as path from "path";
import * as url from "url";
let win: BrowserWindow;
app.on("ready", createWindow);
app.on("activate", () => {
if (win === null) {
 createWindow();
}
});
function createWindow() {
 win = new BrowserWindow({ width: 800, height: 600 });
 win.loadURL(
url.format({
  pathname: path.join(__dirname, `/../../dist/angular-electron/index.html`),
  protocol: "file:",
  slashes: true
})
);
win.webContents.openDevTools();
win.on("closed", () => {
win = null;
});
}

我希望这可以解决您的问题。...编码愉快。谢谢