电子:从main调用渲染器功能 [英] Electron: Call renderer function from main
问题描述
我在本地存储中有一些数据必须在 app.quit()
上删除。但是我从主要过程中看不到这样做。
I have some data in the localstorage that has to be deleted on app.quit()
. But I see no way to do so from the main process.
是否可以从 main $ c $中调用
渲染器
函数c>?
Is there a way to call a renderer
function from main
?
我知道 var remote = require('remote');
,但似乎只能解决
I know about var remote = require('remote');
but it seems to go only in the wrong direction.
推荐答案
您可以通过webContents.send从主进程向渲染器进程发送消息,如此处的文档: https: //github.com/atom/electron/blob/master/docs/api/web-contents.md#webcontentssendchannel-arg1-arg2- 。
You can send messages from the main process to a renderer process via webContents.send as called out in the documentation here: https://github.com/atom/electron/blob/master/docs/api/web-contents.md#webcontentssendchannel-arg1-arg2-.
这是直接从文档中执行的操作:
Here is how you do it straight from the docs:
在主要过程中:
// 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中:
In index.html:
<!-- index.html -->
<html>
<body>
<script>
require('electron').ipcRenderer.on('ping', function(event, message) {
console.log(message); // Prints "whoooooooh!"
});
</script>
</body>
</html>
请注意,它是异步的。我不确定这会如何影响您的特定解决方案,但这至少应该使您重新回到渲染器过程。
Note it is asynchronous. I am not sure how that affects things with your particular solution, but this should at least get you talking back to the renderer process.
这篇关于电子:从main调用渲染器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!