I'm trying to prototype an Electron app using Angular 2 (configured with the latest webpack-based angular cli) for the gui, but I'm stuck since I don't get how to import Electron api in my angular2 components. Specifically I want to be able to open a new BrowserWindow at the click on a button in the ui... so:
<button type="button" (click)="openNewWindow()">
open
</button>
and in my component:
openNewWindow() {
let appWindow = new BrowserWindow({width: 800, height: 600});
appWindow.loadUrl('http://www.google.com');
}
but... how can I import BrowserWindow?!
By using:
import { BrowserWindow } from 'electron';
I get a "no module error" and by following the answer to this question: Webpack cannot find module 'electron' I get:
syntax error near unexpected token ( var electron = require('./')
What should I do?
ps. by running "electron .
" without the BrowserWindow
import the app is working properly
Run the command npm install electron @types/electron
Then import it normally using
import { ipcRenderer } from 'electron'
.
If you run into any problems, try to run npm eject
, a webpack.config.js will be generated, add "target": "electron-renderer" at the top of module.exports
Set this in index.html
<script>
var electron=require("electron");
</script>
Add this line in typings.d.ts file
declare var electron: any;
Use inside the component like this example:
const {ipcRenderer, clipboard} = electron;
clipboard.writeText('Electron is ready!!');
I tried to work with angular-cli and Electron and was not able to make them to work together. It's the reason why I started the following project: https://github.com/osechet/angular-tour-of-heroes-webpack
It integrates Angular 2 with webpack and Electron. It's based on the Angular 2 tutorial (with unit tests). When running Electron in dev mode (npm run start.desktop
), it livereloads the sources.
To complete the answer given by chaouechi souhail.
As I understand his answer aims to solve the situation where the angular app is directly embedded in the electron app. If for some reason you are using two separate projects whereof one is the electron app which embeds the other as a web app using one of electron's webview components you might find the following approach helpful aswell.
- In your electron app you'll have something like
<webview id="webview" src="http://localhost:4200/" nodeintegration></webview>
In your angular project you
- install the electron nodejs module, ie
npm install electron
. The types module mentioned by chaouechi might suffice, I do not know. - you eject the webpack config through
ng eject
- in webpack's config (
webpack.config.js
) you define electron as an external module such that webpack does not attempt to build it into the ng app, extend the exports as below
module.exports = { //... externals: { electron: "require('electron')", // tell's webpack how to import the electron module within your angular app after being packed //... }, //... }
- Now, in your ng components, it should be possible to include electron classes as follows:
import { remote } from "electron";
- install the electron nodejs module, ie
来源:https://stackoverflow.com/questions/40236146/how-to-import-electron-in-angular-2-using-angular-cli