首页 > 解决方案 > 电子在预标签内加载html

问题描述

我最近创建了一个电子/角度应用程序,它在内部运行以下内容main.ts

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

当电子应用程序加载时,以下显示快照 不知何故,html 正在加载到 html 内的<pre>标签内。我已经检查__dirname/dist并且该文件夹包含所有正确的文件(即 index.html、main.js 等)。有没有人见过与电子类似的东西?如果是这样,关于发生了什么的任何线索?

标签: angularelectron

解决方案


虽然不是很明显,但我解决了我的问题——我的win对象抛出警告,因为我使用的是服务人员,因此没有正确加载 html。

解决方案:使用nodeIntegrationInWorker: true属性。一旦添加,一切都像魔术一样工作。

  // Create the browser window.
  win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nodeIntegration: true,
      nodeIntegrationInWorker: true
    }
  });

推荐阅读