使用 Electron 的角度路由

时间:2021-01-17 23:56:01

标签: angular electron

我无法让 <router-outlet></router-outlet> 在我的 Angular 电子应用程序中呈现任何内容。如果我运行 ng serve 那么我可以看到路由器在工作,但在我运行时却看不到 electron . 这是一个空白的应用程序。 我的 index.html 文件有 <base href="./">,我的 main.js 文件在下面。我该怎么做才能使电子的角度布线很好地发挥作用?感谢您的帮助!

main.js

const { app, BrowserWindow } = require('electron')
const url = require("url");
const path = require("path");

let mainWindow

function createWindow() {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  mainWindow.loadURL(
    url.format({
      pathname: path.join(__dirname, `/dist/index.html`),
      protocol: "file:",
      slashes: true
    })
  );
  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

app.on('activate', function () {
  if (mainWindow === null) createWindow()
})

1 个答案:

答案 0 :(得分:0)

您确定 Angular 应用程序甚至被加载到浏览器中吗? Electron docs 提到您需要在窗口准备好后显示它:

mainWindow.once('ready-to-show', () => {
   mainWindow.show();
}

其他一些调试技巧:

  1. 设置断点以确保您的代码正在运行
  2. 完全删除路由以确保您可以只显示一个简单的 angular app.component
  3. 如果您的路由由于某种原因不匹配,请确保您没有找到配置了角度路由器的页面