electron implements communication between windows

Goal : When using electron-vue to develop a desktop application, a new window is created. It is hoped that the value of the main window can be used in the new window, which involves the transfer of values ​​between windows.

Implementation :

  1. The rendering process of window A sends a message to the main process
const sendMsgMain = (msg) => {
    
    
    ipcRenderer.send('sendMsgMain', msg)
}
  1. After the main process receives the message, it sends the message to the rendering process of window B.
ipcMain.on('sendMsgMain', (e, msg) => {
    
    
	childWin.webContents.send('msgFromMain', msg)  // childWin为B窗口
})
  1. Window B rendering process receives main process messages
const getMsgChild = () => {
    
    
    ipcRenderer.on('msgFromMain', (e, message) => {
    
    
        console.log(message)
    });
}
  1. Execute getMsgChild in the component loaded in window B to monitor, create a button click event in the component loaded in window A, and trigger sendMsgMain.

Guess you like

Origin blog.csdn.net/Luo_LA/article/details/129957951