新鲜的新Angular 8 / Electron 5应用上的白屏

时间:2019-05-31 18:44:58

标签: angular electron

我正在构建和运行Angular 8 / Electron 5桌面应用程序。我认为安装正确之后,运行该应用程序将显示白屏。

使用:
电子5.0.2
Angular CLI 8.0.1
节点10.16.0
macOS Mojave 10.14.5
...

ng new my-app
npm i -D electron

package.json

{
  ...
  "main": "main.js", //<-- ADDED
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron": "ng build --baseHref=./ && electron ." //<-- ADDED
  }
  ...
}

main.js

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

let win;

function createWindow() {
  win = new BrowserWindow({ width: 800, height: 600 });

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

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

app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (win === null) {
    createWindow();
  }
});

我还将 angular.json 中的outputPath更改为“ dist”

  {
    ...
    "outputPath": "dist"
    ...
  }

npm run electron

启动了应用

打开应用程序后,我看到了空白的空白屏幕。检查后,我可以看到正文和<app-root>元素,但是我在页面上看到的只是空白的空白屏幕。

ng new my-app时,我在CLI中尝试了有和没有路由启用标志。

在电子安全警告之前的电子应用控制台中获取以下错误:

runtime-es2015.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
styles-es2015.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
main-es2015.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
polyfills-es2015.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
vendor-es2015.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

2 个答案:

答案 0 :(得分:1)

通过将 tsconfig.json 中的target更改为es5,我的电子应用程序开始工作。

{
  "compileOnSave": false,
  "compilerOptions": {
    "importHelpers": true,
    "module": "esnext",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5", <-- HERE
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

在此之前,更新Angular 8后,我也得到了黑屏(白屏)。现在看来Angular在es5es2015中都建立了电子,电子不喜欢那样。我想这会在将来解决。

答案 1 :(得分:0)

该错误似乎是由于脚本元素上的type =“ module”属性引起的,至少这是我发现的主要区别。

要修复此问题,Electron上下文中还有另一种选择,那就是仅生成es2015文件(导致更小且可能更快的脚本)。

基于以下内容,您可以通过为所有都支持es2015的浏览器设置Browserslist来实现此目的。 Angular-cli 8 - Is it possible to build only on es2015?

在电子环境中,最好的方法是将浏览器列表设置为您的电子版本:

 "browserslist": [
    "Electron 5.0.1"
  ],

这对我有用,我想这可能比降级到es5更好。好吧,至少如果电子是唯一目标,那么如果您还计划在网络版本上发布您的应用程序(或为电子和网络配置不同的版本),将其降级到es5可能更安全。