简体   繁体   English

如何在电子桌面应用程序中通过 IpcRenderer 从角度特征组件向电子主进程发送消息

[英]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.所以预计它是undefinednull或类似的东西。

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.

相关问题 如何电子 main.ts 调用或发送消息到角度组件 - How to electron main.ts call or send message to angular component 如何使用 observables 在 electron ipcMain 和 angular ipcRenderer 之间异步发送数据 - How to send data between electron ipcMain and angular ipcRenderer asynchronously with observables Angular &amp; Electron:ipcRenderer - __dirname 未定义 - Angular & Electron: ipcRenderer - __dirname is not defined 使用 .net、angular 和 electron 的桌面应用程序 - Desktop application with .net, angular and electron 从 Electron 接收 ipcRenderer.on 后,Angular 2 视图未更新 - Angular 2 view not updating after receiving ipcRenderer.on from Electron 如何将Electron ipcRenderer集成到基于TypeScript的Angular 2应用程序中? - How to integrate Electron ipcRenderer into Angular 2 app based on TypeScript? 如何在 Angular-Electron 中监听从主进程到渲染器进程的事件 - How to listen for an event from the main process to the renderer process in Angular-Electron 使用电子将 angular 4 应用程序转换为桌面应用程序 - convert angular 4 application to desktop application using electron 如何从角度2组件打开电子对话框? - How to open an Electron dialog from angular 2 component? 将 JSON 数据从 Angular 组件传递到 Electron main.ts - Passing JSON data from Angular component to Electron main.ts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM