【问题标题】:cannont send message through renderer process in electron无法通过电子中的渲染器进程发送消息
【发布时间】:2020-09-14 20:28:22
【问题描述】:

我正在使用电子构建一个简单的桌面应用程序,但这里的问题是,每当我尝试向名为“test”的频道发送消息时,它都可以工作,但同样,如果我尝试向“jest”频道发送消息,则会出现没有输出我正在使用反应和电子我启用了节点集成这里是代码

    mainWindow = new BrowserWindow({
        width: 1200, 
        height: 600,
        webPreferences: {
            nodeIntegration: true,
            preload: path.join(__dirname, '\\preload.js')
        },
    });

    mainWindow.loadURL(
        electronIsDev
            ? 'http://localhost:3000'
            : `file://${path.join(__dirname, '../build/index.html')}`
    );

    mainWindow.on('closed', () => {
        mainWindow = null;
    })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
})

electron.ipcMain.on('test', (event) => {
    console.log("listener is working");
    getWeekList(event);
})

这就是我所说的


console.log('preload is working');

const test = () => {
    console.log("test here");
    ipcRenderer.send('test');
}


const jest = () => {
    console.log("jest here");
    ipcRenderer.send('jest');
}


module.exports = {test, jest};

我做错了什么

【问题讨论】:

    标签: reactjs electron


    【解决方案1】:

    .on()的第一个参数是一个channel,它是你如何识别ipcRenderer和ipcMain之间的连接。它们必须相同才能在它们之间进行交谈。

    此行正在寻找"test",您需要为"jest" 制作另一行。

    electron.ipcMain.on('test', (event) => {
        console.log("listener is working");
        getWeekList(event);
    });
    
    // add this one
    electron.ipcMain.on('jest', (event) => {
        console.log("listener is working");
        getWeekList(event);
    });
    

    【讨论】:

    • 我尝试过更改,但唯一的测试作品向频道 jest 发送消息根本不起作用
    • @AryanChoudhary 你的完整渲染器/​​用户界面/脚本页面是什么样的?
    【解决方案2】:

    嗯,通过将一些频道调用移到主脚本的顶部解决了这个问题。我不知道是什么原因造成的,但现在一切正常

    【讨论】:

      猜你喜欢
      • 2017-03-25
      • 2018-09-25
      • 2022-01-11
      • 1970-01-01
      • 2016-07-05
      • 2021-12-25
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      相关资源
      最近更新 更多