【问题标题】:Can't communicate electronjs-angularjs using IPC无法使用 IPC 通信 electronjs-angularjs
【发布时间】:2019-10-09 07:29:37
【问题描述】:

我是从 electronjs 桌面应用开始的,所以我决定使用 angularjs 作为视图。

现在,当我尝试使用 eletron ipc 在这两者之间进行通信时,我得到 Cannot read property 'send' of undefined

这是我到目前为止所做的:

main.js -- electronjs

var ipc = require('ipc');
ipc.send('asynchronous-message', 'ping');

app.controller.js -- angularjs

const ipcRenderer = require('electron').ipcRenderer;
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong"

ipcRenderer.on('asynchronous-reply', function(event, arg) {
    console.log(arg); // prints "pong"
});
ipcRenderer.send('asynchronous-message', 'ping');

我显然做错了什么,谁能帮我一把?

【问题讨论】:

  • undefined 是一个原语,因此没有属性。这就是它没有send 属性的原因。当变量没有赋值时,代码应该避免访问它们的属性。
  • 我知道。我需要有人指导我如何在电子和 angularjs 之间建立通信。

标签: javascript angularjs electron webkit desktop-application


【解决方案1】:

知道了!!!

ma​​in.js 中创建 BrowserWindow 时设置 nodeIntegration true

win = new BrowserWindow({
show: false,
webPreferences: {
    nodeIntegration: true
  }
});

这允许 angularjs 使用 require(),所以在 app.controller.js 我只是这样做了

const { ipcRenderer } = require('electron')
  // In renderer process (web page).
  console.log(ipcRenderer.sendSync('synchronous-message', 'ping')) // prints "pong"

  ipcRenderer.on('asynchronous-reply', (event, arg) => {
    console.log(arg) // prints "pong"
  })
  ipcRenderer.send('asynchronous-message', 'ping')
}

结果它在控制台中打印: 乒乓 乒乓

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 2012-03-12
    • 2012-04-02
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2016-04-26
    • 2018-05-13
    相关资源
    最近更新 更多