我在本地存储中有一些数据必须在app.quit()
上删除。但是我从主流程中看不到这样做。Electron:主要调用渲染器函数
有没有办法从main
调用renderer
函数?我知道var remote = require('remote');
,但它似乎只走错了方向。
我在本地存储中有一些数据必须在app.quit()
上删除。但是我从主流程中看不到这样做。Electron:主要调用渲染器函数
有没有办法从main
调用renderer
函数?我知道var remote = require('remote');
,但它似乎只走错了方向。
您可以通过webContents.send将主流程中的消息发送到渲染器进程,如文档中所示:https://github.com/atom/electron/blob/master/docs/api/web-contents.md#webcontentssendchannel-arg1-arg2-。
这里是你如何从文档做直:
的主要工序:
// In the main process.
var window = null;
app.on('ready', function() {
window = new BrowserWindow({width: 800, height: 600});
window.loadURL('file://' + __dirname + '/index.html');
window.webContents.on('did-finish-load', function() {
window.webContents.send('ping', 'whoooooooh!');
});
});
index.html中:
<!-- index.html -->
<html>
<body>
<script>
require('electron').ipcRenderer.on('ping', function(event, message) {
console.log(message); // Prints "whoooooooh!"
});
</script>
</body>
</html>
值得注意的是异步的。我不确定这是如何影响您的特定解决方案的,但这至少应该让您回到渲染器流程。
你可以在你的主要过程中使用BrowserWindow.webContents.executeJavaScript像这样:
// will print "whoooooooh!" in the dev console
window.webContents.executeJavaScript('console.log("whoooooooh!")');
虽然你可能认为这是一个有点凌乱/肮脏的方式,它的工作原理。而且它不需要在渲染过程中设置任何东西,这大大简化了我的工作。
如果你只是想调用一个特定的方法,它可能会更快地写这种方式。
很酷,谢谢。我稍后再检查一下 –