[英]how to send message from angular feature component to electron main process via IpcRenderer in electron desktop application
I have created angular 8 and electron desktop application.我创建了 angular 8 和电子桌面应用程序。 When I am sending a message from the root component to main.js file of electron via ipcRenderer it is working fine.
当我通过 ipcRenderer 从根组件向电子的 main.js 文件发送消息时,它工作正常。 But when I am sending a message from a newly generated component to main.js file it is throwing an error of "Cannot read property 'send' of undefined".
但是,当我从新生成的组件向 main.js 文件发送消息时,它会抛出“无法读取未定义的属性‘发送’”的错误。
Below is my write.component.html下面是我的 write.component.html
<button (click)="write()">Click me</button>
my write.component.ts file我的 write.component.ts 文件
import { Component, OnInit } from '@angular/core';
import { IpcRenderer } from 'electron';
export class WriteComponent implements OnInit {
public ipcrenderer: IpcRenderer
ngOnInit() { }
write() {
this.ipcrenderer.on('message-to', (event, arg) => {
console.log("message-to" + arg)
});
this.ipcrenderer.send('message-from', 'hellooooooooooooo')
}
}
my main.js file我的 main.js 文件
const { app, BrowserWindow, ipcMain } = require('electron')
const url = require("url");
const path = require("path");
const http = require('http')
let mainWindow
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, `/dist/index.html`),
protocol: "file:",
slashes: true
})
);
// Open the DevTools.
mainWindow.webContents.openDevTools()
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (mainWindow === null) createWindow()
})
ipcMain.on('message-from', (event, arg) => {
console.log(arg)
console.log("++++++++++++++++++++++++" + arg)
event.sender.send('message-to', "world");
})
With this line public ipcrenderer: IpcRenderer
You are declaring a variable with type IpcRenderer
.使用这一行
public ipcrenderer: IpcRenderer
您正在声明一个类型为IpcRenderer
的变量。
But you do not assign any value to that variable.但是您没有为该变量分配任何值。 So it is expected for that to be
undefined
or null
or such kind of things.所以预计它是
undefined
或null
或类似的东西。
So you should assign a value to it OR the value should be injected from the constructor of the newly generated component.因此,您应该为其分配一个值,或者该值应该从新生成的组件的构造函数中注入。
Maybe you can show me the codes in the root component.也许您可以向我展示根组件中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.