首页 > 解决方案 > Electron:从 main.js 向 index.js 发送消息

问题描述

在我继续之前,我是电子新手,昨天才开始使用它,所以请详细说明你的答案。

所以我正在创建一个记事本应用程序。我想在单击子菜单时发送消息。这个子菜单和 BrowserWindow 都在main.js文件中。每当我单击子菜单时,它应该发送一条消息,然后由名为 index.js 的脚本接收。(此脚本包含在我index.html的窗口中加载的文件中)。

这是我试图做的:

// main.js submenu
{
    'label': 'Open',
     click() {
         dialog.showOpenDialog({properties: ['openfile']}).then(result => {
             win.webContents.send('open', result.filePaths[0])
        })
    }
}
// index.js
const {ipcRenderer} = require('electron')

ipcRenderer.on('open', (event, file) => {
    console.print(file)
    console.log('Recieved')
})

但是,这似乎不起作用。

标签: javascriptelectron

解决方案


推荐阅读