electronicjs中的ipc通信问题

时间:2018-06-27 06:52:37

标签: javascript sql sqlite electron

我正在构建一个基本上读取sqlite3数据库文件的应用程序。我有两个渲染过程。这两个渲染过程都是在主过程中创建的。第二个渲染过程是通过在第一个渲染过程中单击特定按钮来创建的,第一个渲染过程在该按钮上向主进程发送ipcRenderer.send()消息。

我的代码能够创建第二个渲染过程,但是无法访问应该获取的数据。这是我的第一个问题,很抱歉,它缺少某些协议。

第一个代码段是主流程(app.js)中的代码

database.all(`SELECT _table_Name as name, _table_json as json
    FROM _table_main WHERE _table_Name = "EWS Equipment Status    Recording"`, (err, rows) => {

if (err) {
  console.log(err.message);
}


mainWindow.webContents.send('query:ResultRows', rows);
});

//第二个片段是第一个渲染过程

    ipcRenderer.on('query:ResultRows',(event,rows) => {

   var btn = document.createElement("BUTTON");
   var t = document.createTextNode(rows[0].name);
   btn.appendChild(t);
   document.body.appendChild(btn);

   var test = rows;
   btn.addEventListener('click', function() {

   ipcRenderer.send('show:popUpContents', test);

   console.log(rows);


   });
   });

现在在主进程(app.js)上监听第二个片段

    ipcMain.on('show:popUpContents',(event,test) =>{

    popWindow = new BrowserWindow({ });


    popWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'pop-up.html'),
    protocol: 'file:',
    slashes: true
    }));


    popWindow.webContents.send('get:popUpContents', test);
    console.log(test);
    console.log(popWindow.webContents);
    popWindow.webContents.openDevTools();
    });

//现在打开了第二个渲染过程,但是它无法访问'test'变量,也无法在块内打印console.log。enter image description here

     ipcRenderer.on('get:popUpContents',(event,test) => {
     debugger;
     console.log(test);
     console.log('hellow world');
   });
   console.log('outside function');

1 个答案:

答案 0 :(得分:1)

一天后,我设法自己解决了这个问题。在使新的浏览器窗口侦听传入的消息之前,需要等待它的“准备加载”事件。