問題描述
我想使用 ipcMain/ipcRenderer 在我的項(xiàng)目上從 Angular 到 Electron 進(jìn)行通信并返回.
I want to use ipcMain / ipcRenderer on my project to communicate from Angular to Electron and back.
Electron 方面非常清楚:
The Electron side is pretty clear:
const
electron = require('electron'),
ipcMain = electron.ipcMain,
;
ipcMain.on('asynchronous-message', function(event, arg) {
console.debug('ipc.async', arg);
event.sender.send('asynchronous-reply', 'async-pong');
});
ipcMain.on('synchronous-message', function(event, arg) {
console.debug('ipc.sync', arg);
event.returnValue = 'sync-pong';
});
但我不知道如何將該 Electron 模塊集成到我的 Angular 2 應(yīng)用程序中.我使用 SystemJS 作為模塊加載器,但我是新手.
But I have no idea how to integrate that Electron module into my Angular 2 app. I use SystemJS as module loader, but I'm a rookie with it.
任何幫助表示贊賞.謝謝.
Any help appreciated. Thanks.
--- 馬里奧
推薦答案
存在沖突,因?yàn)?Electron 使用 commonjs 模塊解析,但您的代碼已經(jīng)使用 systemjs 規(guī)則編譯.
There is conflict, because Electron use commonjs module resolving, but your code already compiled with systemjs rules.
兩種解決方案:
穩(wěn)健的方式.注冊對象 require
返回:
<script>
System.set('electron', System.newModule(require('electron')));
</script>
這是最好的,因?yàn)?renderer/init.js
腳本會在啟動時(shí)加載該模塊.SystemJS 必須只接受它,而不是加載.
This is the best, because renderer/init.js
script loads that module on start. SystemJS have to take it only, not loads.
替代方式.在聲明中使用骯臟的技巧.
Alternative way. Use dirty trick with declaration.
在index.html
中獲取電子實(shí)例:
<script>
var electron = require('electron');
</script>
以這種方式在您的 typescript
文件中聲明它:
Declare it inside your typescript
file this way:
declare var electron: any;
自由使用)
electron.ipcRenderer.send(...)
這篇關(guān)于如何將 Electron ipcRenderer 集成到基于 TypeScript 的 Angular 2 應(yīng)用程序中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!