How to solve TypeError: Cannot read property 'send' of undefined in app.on

How to solve TypeError: Cannot read property 'send' of undefined in app.on

我有一个错误:

TypeError: Cannot read propety 'send' of undefind on start of my program. How to correctly trigger app.on without errors?

import * as electron from "electron";
const {app, BrowserWindow,ipcRenderer,ipcMain} = electron;

var correspondingWindow = null;
let mainWindow;

ipcMain.on('newWindow', function (e, category) {

//some code with switch Window_1
 
});

app.on('ready', function () {
  "use strict";
  ipcRenderer.send('newWindow', 'Window_1');
});

我觉得这可能是另一个问题的骗局,但是 ipcRenderer 不能在主进程中使用。看到 docs 说:

Process: Renderer

如果要从主进程向主进程发送消息,可以使用ipcMain.emit,或者直接调用函数即可。为什么在这种情况下发送消息?