I构建一个角应用,然后使用电子包装。
我的dir结构看起来像这样:
项目
| - 电子
--- |-main.js
----- | -Index.html(以及从WebApp/Dist复制的其他文件)
| -webapp(Angular App)
--- |-src
----- |-App
------- | - 服务
ipcMain
和
ipcRenderer
),但挂断了! 然后我知道
webcontent.executeJavascript();
我在Angular中提供了一项服务,该服务具有各种功能,例如eventFromHost()
&
sendMessage()
。我如何使用WebContent或任何其他方法从Electron的main.js调用此功能?
您应该使用
ipcMain
ipcRenderer
:
:Angular:
ipcMain.send('foo', data);
电子:
ipcMain.on('foo', (event, data) => {
// Do what you want with data.
});
或使用ipcRenderer
ipcMain
要从角到电子进行通信)。
如果您想查看一个现场示例,请在github上查看this main.jsfile
,以及与之通信的服务
import { Injectable } from '@angular/core';
import { ipcRenderer } from 'electron';
@Injectable({
providedIn: 'root'
})
export class IpcService {
private _ipc: typeof ipcRenderer | undefined;
constructor() {
this._ipc = window.require('electron').ipcRenderer;
}
send(message:string){
this._ipc?.send(message);
}
}