问题
Angular-cli doesn't accept the electron inside components. how to use electron browser window inside components in angular-cli? I got an error fs.existsync like this. Is there any other options to use electron with angular2 components?
var electron = require('electron');
or
import electron from 'electron';
回答1:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
<title>Workdesk 2.0</title>
<!--<base href="./">-->
<base href="./">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.css">
<script src="assets/jquery.js"></script>
</head>
<body style="margin: 0px;">
<app-root> </app-root>
<script>
var electron = require('electron');
var BrowserWindow = require('electron').remote.BrowserWindow;
var ipcMain = require('electron').remote.ipcMain;
var shell = require('electron').shell;
var Positioner = require('electron-positioner');
var path = require('path');
var gkm = require('gkm');
var fs = require('fs');
var os = require('os');
var path = require('path');
var base64Img = require('base64-img');
var ip = require('ip');
</script>
</body>
</html>
This will help you to communicate with electron in any of the script languages like angular2,react js, node js etc.,
回答2:
Once you install ngx-electron via npm, you can create browserwindows inside of an angular component like this: (Working as of Angular 7)
import { ElectronService } from 'ngx-electron';
constructor(
private _electronService: ElectronService){}
var BrowserWindow = this._electronService.remote.BrowserWindow
var win = new BrowserWindow({
width: 600,
height: 840,
})
win.webContents.openDevTools()
回答3:
You can only do this by initialize electron variables inside index.html and you can communicate wherever you want inside typescript. Use electron variables inside TS.
来源:https://stackoverflow.com/questions/42711332/how-to-use-electron-browser-window-inside-components-in-angular-cli