简体   繁体   中英

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

Not allowed to load local resource: 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

ELECTRON/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

{
  "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. 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 ( 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM