简体   繁体   English

不允许加载本地资源-Angle7和Electronic

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

I am having issues working with electron. 我在使用电子时遇到问题。 I am able to load my project perfectly with ng serve, but when I try to open with electron it shows this error in the developer tools 我可以用ng serve完美地加载我的项目,但是当我尝试用电子打开时,它在开发人员工具中显示此错误

Not allowed to load local resource: file:///C:/Users/moise/Documents/Code/electron/electron-routing-test/electron/dist/dist/index.html 不允许加载本地资源: file:///C:/Users/moise/Documents/Code/electron/electron-routing-test/electron/dist/dist/index.html

I have seen that some people fix it by changing their file path or package.json config but I can't find a fix 我已经看到有人通过更改文件路径或package.json配置对其进行了修复,但找不到修复程序

ELECTRON/MAIN.TS 电子/ 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 FILE 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"
  }
}

You have two "dist" in your URL. 您的网址中有两个“ dist”。 I assume that you have this problem on production (When you try to open built electron app). 我假设您在生产中遇到此问题(当您尝试打开内置的电子应用程序时)。

You need IF statement to load appropiorate URL. 您需要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