我写了一个非常非常基本的电子应用程序 - 标准的hello world类型,你基本上有一个HTML文件,上面写着“Hello,World” - 它位于电子内的“app”目录中,然后通过main加载运行应用程序时的.js .
现在,假设我希望能够与我的应用程序中的javascript中的任何一个进程(主要或渲染器,最好是两个!)进行通信,可以这样做吗?我无法在网上找到任何关于它的信息 - 但我的主要问题可能是我甚至不知道首先要搜索什么 . 我对Electron很新 .
我想你在谈论主要流程和其他浏览器窗口 .
您可以使用 BrowserWindow.webContents.send(channel[, arg1][, arg2][, ...]) 将消息从主进程发送到浏览器窗口,并使用 ipcRenderer 接收它 . 举个例子:
BrowserWindow.webContents.send(channel[, arg1][, arg2][, ...])
ipcRenderer
主要流程:
subWindow.webContents.send("foo","bar");
BrowserWindow 名为 subWindow :
BrowserWindow
subWindow
var ipc=require("electron").ipcRenderer; ipc.on("foo",(event, arg1) => { console.log(arg1); //Outputs "bar" });
如果要将数据从浏览器窗口发送到主进程,请使用 remote.app.emit . 使用 app.on 接收它 . 同样的例子:
remote.app.emit
app.on
var app=require("electron").app; app.on("test",(arg) => { if (arg=="hey!") console.log("ha!"); }
子窗口:
require("electron").remote.app.emit("test","hey!");
1 回答
我想你在谈论主要流程和其他浏览器窗口 .
您可以使用
BrowserWindow.webContents.send(channel[, arg1][, arg2][, ...])
将消息从主进程发送到浏览器窗口,并使用ipcRenderer
接收它 . 举个例子:主要流程:
BrowserWindow
名为subWindow
:如果要将数据从浏览器窗口发送到主进程,请使用
remote.app.emit
. 使用app.on
接收它 . 同样的例子:主要流程:
子窗口: