問題描述
我在本地存儲中有一些數據必須在 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
調用 renderer
函數?
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.
這篇關于Electron:從 main 調用渲染器函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!