angular - 不允许加载 Electron 中的本地资源错误- Angular v7.0.1

标签 angular electron

我正在尝试首次使用Electron设置我的应用程序。完成了添加main.js,更改index.html,更改package.json的初始步骤。当我尝试使用npm run electron-build命令运行应用程序时,出现以下屏幕。

Error-while-running-electron-app

我无法运行我的应用程序。基本上这不是一个错误,而只是缺乏信息。任何对此的帮助将不胜感激。 TIA。

main.js

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

let win;

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({
    width: 600, 
    height: 600,
    backgroundColor: '#ffffff',
    icon: `file://${__dirname}/dist/assets/logo.png`
  })


  win.loadURL(`file://${__dirname}/dist/index.html`)

  win.webContents.openDevTools()

  // Event when the window is closed.
  win.on('closed', function () {
    win = null
  })
}

// Create window on electron intialization
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {

  // On macOS specific close process
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // macOS specific close process
  if (win === null) {
    createWindow()
  }
})

package.json
 "name": "angular-electron-first-app",
  "version": "0.0.0",
  "main": "main.js",
  "scripts": {
    "ng": "ng",
    "start": "electron .",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron": "electron .",
    "electron-build": "ng build --prod && npm run electron ."
  },

index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularElectron</title>
  <base href="./">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>

</body>
</html>

其他详情:
Angular v7.1.0
RxJS v6.5.2
NPM v6.5.0

最佳答案

结合使用loadFile和本地资源

win.loadFile(filePath[, options])

filePath String

options Object (optional)
    query Object (optional) - Passed to url.format().
    search String (optional) - Passed to url.format().
    hash String (optional) - Passed to url.format().

Returns Promise - the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load).

Same as webContents.loadFile, filePath should be a path to an HTML file relative to the root of your application. See the webContents docs for more information.

关于angular - 不允许加载 Electron 中的本地资源错误- Angular v7.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57690039/

相关文章:

angular - Uncaught TypeError : r. existsSync 不是 Angular 6 和 Electron 桌面应用程序中的函数

javascript - 如何使用新的 html 文件打开更新 Electron 浏览器窗口

typescript - Angular2 - Http POST 请求参数

angular - Typescript中的字符串插值,用变量替换 'placeholders'

javascript - Electron js没有正确加载jquery

javascript - 尝试导入项目时出现错误 "Strict MIME type checking is enforced for module scripts per HTML spec"

javascript - Angular 5 - 带选择的 JSON

angular - 树 : How to keep opened states when tree updated

angular2 在 init 上设置事件选项卡

vue.js - Electron JS事件多次触发