Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Angular 2 with Electron (Angular CLI)

I generated a new Angular 2 project using the new Angular CLI. Now I don't want to use Angular in browser but in an electron app. Therefore I created a file for the main process of electron and after building my Angular app with ng build in terminal, the app was not working as expected.

File for Main Process of Electron:

var electron = require("electron");
var {app, BrowserWindow} = electron;

app.on('ready', () => {
    var mainWindow = new BrowserWindow();
    mainWindow.loadURL(`file://${__dirname}/dist/index.html`);
});

Error in DevTools:

file:///vendor/es6-shim/es6-shim.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/reflect-metadata/Reflect.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/systemjs/dist/system.src.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/zone.js/dist/zone.js Failed to load resource: net::ERR_FILE_NOT_FOUND
index.html:22 Uncaught ReferenceError: System is not defined

I also know that you essentially need a web server to run Angular 2 Applications and I think my app does not work because Electron loads the App via the file:// protocol.

But I really want to use Electron in conjunction with Angular 2; so my question is if this is possible and if so how I need to modify the generated Angular Template from the cli utility.

like image 561
HansMu158 Avatar asked Jul 07 '16 18:07

HansMu158


People also ask

Can you use Angular with Electron?

Building Desktop Applications With ElectronYou can use Electron to build desktop applications on Windows, Mac, and Linux. By default, you can test an Angular application using a web browser via the ng serve command. You can configure your Angular application to also open in a desktop window instead of a web browser.

Do you need node to run Electron?

To use Electron, you need to install Node. js. We recommend that you use the latest LTS version available.


2 Answers

You can also change

<base href="/">

to

<base href="./">
like image 166
VtoCorleone Avatar answered Oct 02 '22 19:10

VtoCorleone


After reading something about the Angular 2 Router yesterday, I figured out that the problem is caused by this line in the index.html

<base href="/">

By replacing it with a dynamically set base everything works as expected.

<script>document.write('<base href="' + document.location + '" />');</script>
like image 35
HansMu158 Avatar answered Oct 02 '22 18:10

HansMu158