簡體   English   中英

不允許加載本地資源-Angle7和Electronic

[英]Not allowed to load local resource - angular7 & electron

我在使用電子時遇到問題。 我可以用ng serve完美地加載我的項目,但是當我嘗試用電子打開時,它在開發人員工具中顯示此錯誤

不允許加載本地資源: file:///C:/Users/moise/Documents/Code/electron/electron-routing-test/electron/dist/dist/index.html

我已經看到有人通過更改文件路徑或package.json配置對其進行了修復,但找不到修復程序

電子/ MAIN.TS

import { app, BrowserWindow } from "electron";
import * as path from "path";
import * as url from "url";

let win: BrowserWindow;

app.on("ready", createWindow);

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

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


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

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

  win.webContents.openDevTools();

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

}

PACKAGE.JSON文件

{
  "name": "electron-routing-test",
  "version": "0.0.0",
  "main": "electron/dist/main.js",

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron": "ng build --base-href ./ && tsc --p electron && electron ."
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.11.0",
    "@angular/cli": "~7.1.4",
    "@angular/compiler-cli": "~7.1.0",
    "@angular/language-service": "~7.1.0",
    "@types/electron": "^1.6.10",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.5.0",
    "electron": "^4.0.6",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}

您的網址中有兩個“ dist”。 我假設您在生產中遇到此問題(當您嘗試打開內置的電子應用程序時)。

您需要IF語句來加載適當的URL。

if ( production ) {
  win.loadURL(`file://${__dirname}/index.html`); // It will load in production mode
} else {
  win.loadURL(`file://${__dirname}/dist/index.html`); // It will load in dev mode, when you run ng-serve
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM