angular - 全新 Angular 8/Electron 5 应用出现白屏

标签 angular electron

我正在构建和运行 Angular 8/Electron 5 桌面应用程序。在我认为正确设置后,运行该应用程序会显示空白屏幕。

使用:
Electron 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
  }
  ...
}

ma​​in.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 启动应用程序

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

在做 ng new my-app 时我在 CLI 中尝试了使用和不使用路由启用标志。

在 Electron 安全警告之前,在 Electron 应用程序控制台中获取这些错误:

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.

最佳答案

我通过将 tsconfig.json 中的 target 更改为 es5 让我的 Electron 应用程序工作。

{
  "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 中都进行了构建, Electron 不喜欢那样。我想这会在未来得到解决。

2019-10-24 更新:

看起来这是在 electron@7.0.0 中修复的。您可以使用该版本定位 es2015。使用 @angular/cli@8.3.14 进行测试。

关于angular - 全新 Angular 8/Electron 5 应用出现白屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56400290/

相关文章:

javascript - 由于不推荐使用Buffer(),因此将DesktopCapturer保存到Electron中的文件中

javascript - $ 未定义 - 将 jQuery 与 electron.js 一起使用

angular - 使用切片管道后如何连接字符串

angular - 如何使 NativeScript-Angular 应用程序在后台运行?

在每个页面上运行的 Angular6 应用程序逻辑

javascript - Electron - 处理输入

node.js - 程序列表中的 Electron 生成器应用程序名称没有 Windows 版本

javascript - 如何使用 [(ngModel)] 双向数据绑定(bind)在 Angular 4 中创建切换开关开关按钮

javascript - Angular,如何通过单击按钮隐藏表格行

javascript - 如何在 Electron 中获取日期格式 - javascript