4
我正在写一个Eletron程序。在程序中有一个由主进程创建的索引窗口(main.js)。在这个窗口中有一个文件列表(图片)。当我点击该列表中的一个文件时,我想启动显示该文件的第二个窗口。 第二个窗口由索引窗口(index.js)的渲染器进程启动。我如何在索引窗口的渲染器进程和第二个窗口的渲染器进程之间进行通信?Electron中的两个渲染器进程之间的通信
代码:
创建从主过程中main.js索引窗口:
let win;
function createWindow(){
// Create the browser window.
win = new BrowserWindow({width: 1024, height: 768, minWidth: 800, minHeight: 600, show: false, icon: 'files/images/icon.png'});
win.loadURL(`file://${__dirname}/files/html/index.html`);
win.once('ready-to-show',() => {
win.show()
})
// Emitted when the window is closed.
win.on('closed',() => {
win = null;
});
}
app.on('ready', createWindow);
在index.html的index.js(渲染处理)开始:
<script src="../javascript/index.js"></script>
在index.js的function create_sprite_window()
被称为它创建了一个子窗口:
const fs = require('fs');
const path = require('path');
const {BrowserWindow} = require('electron').remote
let child_windows = [];
function create_child_window(URL, width, height){
let rem_win = require('electron').remote.getCurrentWindow();
let new_win = new BrowserWindow({width: width, height: height, minWidth: 400, minHeight: 300, show: false, parent: rem_win, minimizable: true, maximizable: true, skipTaskbar: true});
child_windows[child_windows.length] = new_win;
console.log(child_windows);
new_win.loadURL(URL);
new_win.once('ready-to-show',() => {
new_win.show()
})
return new_win;
}
function create_sprite_window(){
new_win = create_child_window(`file://${__dirname}/../html/sprite_manager.html`, 800, 400);
}
子窗口存储在数组child_windows
中。
是否有可能再发送的图像的路径转移到第二窗口,或者可选地,编辑所述第二窗口的<img>
标签从(在第二窗口getElementById.src = path;
设置<img>
标签的源至图像)索引窗口?
它可能会工作,但它不会是电子的最佳解决方案。一种方法是使用'ipcMain'和'ipcRenderer'模块。通过您的索引文件渲染器,您可以将文件路径发送到主进程然后主进程可以使用该文件路径打开一个新窗口 –