【问题标题】:how to send message from angular feature component to electron main process via IpcRenderer in electron desktop application如何在电子桌面应用程序中通过 IpcRenderer 从角度特征组件向电子主进程发送消息
【发布时间】:2020-03-13 09:10:41
【问题描述】:

我已经创建了 angular 8 和电子桌面应用程序。当我通过 ipcRenderer 从根组件向电子的 main.js 文件发送消息时,它工作正常。但是,当我从新生成的组件向 main.js 文件发送消息时,它会抛出“无法读取未定义的属性'发送'”的错误。

下面是我的 write.component.html

<button (click)="write()">Click me</button>

我的 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')
  }
}

我的 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");
})

【问题讨论】:

    标签: angular electron


    【解决方案1】:

    通过public ipcrenderer: IpcRenderer 这一行,您声明了一个类型为IpcRenderer 的变量。

    但是您确实为该变量分配任何值。所以预计会是undefinednull 或类似的东西。

    所以你应该给它赋值或者这个值应该从新生成的组件的构造函数中注入。

    也许你可以告诉我根组件中的代码。

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 2021-08-11
      • 2021-12-17
      • 1970-01-01
      • 2021-02-12
      • 2019-12-04
      • 2011-08-05
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多