【问题标题】:Electron ipcRenderer message between two windows doesn't work两个窗口之间的电子 ipcRenderer 消息不起作用
【发布时间】:2019-02-20 13:29:19
【问题描述】:

我正在尝试将消息从模式发送回浏览器窗口,以便使用从模式返回的数据对其进行更新。

模态框有一个表格,当您单击一行时,行 ID 通过 ipcRenderer 消息发送,但该消息似乎没有到达那里,因为控制台中没有记录任何内容。

我做错了什么?

浏览器窗口(渲染器)

const { remote } = require('electron');
const ipcRenderer = require("electron").ipcRenderer;

function openModal() {
    let win = new remote.BrowserWindow({
        parent: remote.getCurrentWindow(),
        modal: true
    })

    win.webContents.openDevTools();
    var theUrl = 'file://' + __dirname + '/modal.html'

    win.loadURL(theUrl);
}

// Open the modal on button click
document.getElementById("button-search-open")
    .addEventListener("click", () => {
        openModal();
    })

// Log the data received from the modal message
ipcRenderer.on('set-row-active-id', (e, args) => {
    console.log(e, args); // Nothing is logged!
 })

模态(渲染器)

 var ipcRenderer = require("electron").ipcRenderer;

// Add event listeners to all table rows
document.querySelectorAll('table tr')
    .forEach(el => el.addEventListener("click", (e) => { rowClickHandler(e) }));

// Send the row id back to the broserwindow on row click
function rowClickHandler(e) {
    let rowId = e.target.parentElement.dataset.id
    ipcRenderer.send('set-row-active-id', rowId);
}

【问题讨论】:

  • 或许this可以帮到你

标签: javascript node.js electron


【解决方案1】:

ipcRenderer.send 用于向主进程发送消息。您想要的是通过contents.send

向特定的webContents 发送消息

由于您的模态窗口应该发送给它的父级,您可以使用win.getParentWindow

// (Modal - Renderer)
const { remote, ipcRenderer } = require('electron')
// ...
function rowClickHandler() {
  let rowId = e.target.parentElement.dataset.id
  remote.getCurrentWindow().getParentWindow().send('set-row-active-id', rowId)
}

【讨论】:

猜你喜欢
  • 2018-07-01
  • 2021-01-29
  • 2020-11-11
  • 2022-01-25
  • 1970-01-01
  • 2023-03-16
  • 2018-08-04
  • 2016-09-27
  • 2021-08-14
相关资源
最近更新 更多