如何修复:“无法加载模块脚本......”Angular 8、Electron 5

Mar*_*n N 2 javascript electron angular

我正在尝试使用 Angular 8 制作一个 Electron 5 应用程序。我已经在线学习了几个教程,但仍然遇到相同的错误。

我已经创建了一个新项目,运行ng serve --open并且运行良好,我获得了默认的 angular 主页。

然后我安装了电子npm install --save-dev electron。我在main.js项目的根目录中创建了我的文件并放置:

const { app, BrowserWindow } = require('electron')

let win;

function createWindow() {
    // Create the browser window.
    win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            nodeIntegration: true
        }
    })

    win.loadFile('dist/myproject/index.html')
    // Open the DevTools.
    win.webContents.openDevTools()
    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()
    }
})
Run Code Online (Sandbox Code Playgroud)

我也将 更改<base href="/"><base href="./">in index.html。我也相应地修改了package.json文件:

  "main": "main.js",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron": "ng build && electron ."
  },
Run Code Online (Sandbox Code Playgroud)

我在运行时遇到的问题npm run 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.
Run Code Online (Sandbox Code Playgroud)

Tho*_*yne 7

Angular 建立在es5和 中es2015,Electron 可能不喜欢优柔寡断。您是否tsconfig.json有合适的目标:

"target": "es5"
Run Code Online (Sandbox Code Playgroud)