I have developed application using angular 4. I need to develop desktop application for this web application . From my initial research i got the best solution is electron. Any one please suggest the steps to convert angular 4 application to electron?
Please suggest!!!
Conclusion. Angular is wonderful and enables developers to build modern web apps. But much of the Angular goodness—components, data-binding, routing and more, can be brought over to desktop apps.
You 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. You can do this using a JavaScript file.
With an improved runtime and great integration with JavaScript and Node. js, Electron JS makes both designing desktop apps and maintaining them on cross platforms easier and better.
Assuming you have a working Angular app, i use the following steps to convert it to an electron web app:
src/index.html
change <base href="/">
to <base href="./">
npm install electron --save-dev
main.js
in root of the project (not in src) (this is where createWindow()
code goes)main.js
points to dist/index.html
(not just index.html
)"main":"main.js"
, to package.jsonadd these to the scripts section of the package.json
"electron": "electron .", // <-- run electron
"electron-build": "ng build --prod && electron ." // <-- build app, then run electron `
run/debug the app with:
npm run electron-build
to build the app:
npm install electron-packager -g
npm install electron-packager --save-dev
then run:
electron-packager . --platform=win32
Example main.js:
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
let win
function createWindow () {
win = new BrowserWindow({width: 800, height: 600}) // load the dist folder from Angular
win.loadURL(url.format({ pathname: path.join(__dirname, 'dist/index.html'), protocol: 'file:', slashes: true }))
// Open the DevTools optionally:
// win.webContents.openDevTools()
win.on('closed', () => { win = null })
}
app.on('ready', createWindow)
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } })
app.on('activate', () => { if (win === null) { createWindow() } })
Access to electron API functions
There's a quick and easy way to gain access to the API, which is through a package called ngx-electron.
Install it from the console:
npm install ngx-electron --save
It has to be added to the imports array in /src/app/app.module.ts:
import { NgxElectronModule } from 'ngx-electron';
@NgModule({
...
imports: [
BrowserModule,
NgxElectronModule // Add it here
],
...
})
export class AppModule { }
To use it, open /src/app/app.component.ts and add the following to the imports:
import { Component } from '@angular/core';
import { ElectronService } from 'ngx-electron';
Then, in the component class, create an instance of it through dependency injection, which gives access to the methods of the API:
export class AppComponent {
constructor(private _electronService: ElectronService) {} // DI
launchWindow() {
this._electronService.shell.openExternal('http://google.co.uk');
}
}
It provides you with the following functionality (Visit their Github for more info):
Simple steps:
dist
directory to electron project (index.html
bundle.js
etc.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With