Electron main process and rendering process value transfer and value transfer between windows

1 The rendering process calls the main process.
 The following is the code of the rendering process:

let { ipcRenderer} = require( 'electron' );
ipcRenderer.send( 'xxx' ); // Called during the rendering process

The following is the code of the main process:

var { ipcMain } = require( 'electron' );

 ipcMain.on("xxx",function () { } )


   2 Value transfer between rendering process and rendering process

Use remote.BrowserWindow to open a new window in a rendering process and pass values ​​to the new window

xxxrWindow = new remote.BrowserWindow( {
			webPreferences: {
				nodeIntegration: true
			},
			show: true
		} );

xxxrWindow.webContents.on( 'did-finish-load', () => {
			xxxrWindow.webContents.send( "data", JSON.stringify(passInfo) );
			remote.getCurrentWindow().close();//关闭当前窗口
		} );

Then add the receiving listening code in the html start Script tag in the new window

	//数据监听
		const ipc = require('electron').ipcRenderer;
		ipc.on('data', (e,arg) => {
			console.log("+++++++++++++++uu+++++++++++++++++++++++++");
			console.log(arg)
		});

Note Note : The current window can be closed only after the message is sent, otherwise the new window opened will not receive the message.

That is, remote.getCurrentWindow().close(); This line of code should be written in the above callback to ensure that the current window is closed after the code is sent. If this line of code is written outside, the window will be closed before the code is sent. Off, so the code sent will not be executed.


FR:hunkXu

Guess you like

Origin blog.csdn.net/qq_15267341/article/details/131944596