![](/img/trans.png)
[英]When running getting: TypeError: Cannot read properties of undefined (reading 'send')
[英]I am getting an error when using ipcRenderer (typeerror cannot read properties of undefined (reading 'send'))
使用 ipcRenderer 时出现错误(类型错误无法读取未定义的属性(读取“发送”))。 我觉得错误在(const mainWindowTemplate)中,我不知道为什么会弹出这个错误,因为其他时候我使用 ipcRenderer 这从未发生过。 我也在 preload.js 中使用 ipcMain.on 时遇到错误。 我还发送了由这两个引起的更多错误的屏幕截图。
主程序
const electron = require("electron");
const url = require("url");
const path = require('path');
const { ipcRenderer } = electron;
const {app , BrowserWindow , Menu , IpcMain} = electron;
require('electron-reloader')(module)
let mainWindow;
let windowForAnswer;
function createWindow(){
mainWindow = new BrowserWindow({
title: "Aspire Education",
titleBarStyle: "hiddenInset",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
preload: path.join(app.getAppPath(), 'preload.js'),
}
});
mainWindow.loadURL(url.format({
pathname: path.join(__dirname , "mainWindow.html"),
protocol: 'file:',
slashes: true
}))
}
app.on('ready' , function(){
console.log("ready");
createWindow();
})
function createWindowQuiz(){
windowForAnswer = new BrowserWindow({
title: "Quiz",
titleBarStyle: "hiddenInset",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
preload: path.join(__dirname, 'mainWindow.js'),
}
});
windowForAnswer.loadURL(url.format({
pathname: path.join(__dirname , "windowForAnswer.html"),
protocol: 'file:',
slashes: true
}))
}
const mainMenuTemplate = [
{
label: 'File',
submenu:[
{
label:'Start Quiz',
click(){
createWindowQuiz();
ipcRenderer.send("send-questions")
}
},
]
}
]
// Add developer tools option if in dev
if(process.env.NODE_ENV !== 'production'){
mainMenuTemplate.push({
label: 'Developer Tools',
submenu:[
{
role: 'reload'
},
{
label: 'Toggle DevTools',
accelerator:process.platform == 'darwin' ? 'Command+I' : 'Ctrl+I',
click(item, focusedWindow){
focusedWindow.toggleDevTools();
}
}
]
});
}
menu = Menu.buildFromTemplate(mainMenuTemplate)
Menu.setApplicationMenu(menu)
预加载.js
const electron = require('electron')
const url = require('url')
const path = require('path')
const { ipcMain } = require('electron')
const {app , BrowserWindow} = electron
let questions = []
function Submit() {
clicked = true
let question = document.getElementById('question').value;
let answer = document.getElementById('answer').value;
if (question.trim() == "" || answer.trim() == "") {
alert("Question or answer can't be blank")
} else {
console.log(question);
console.log(answer);
questions.push(question.trim())
questions.push(answer.trim())
console.log(questions)
}
document.getElementById('question').value = "";
document.getElementById('answer').value = "";
return false;
}
ipcMain.on('send-questions' , () => {
console.log("Hello World!")
})
您正在尝试访问预加载脚本中的 ipcMain。
ipcMain 仅在主进程中可用。
参见https://www.electronjs.org/docs/latest/api/ipc-main/
这就是为什么 ipcMain 未定义并且您收到错误无法读取未定义的属性的原因。
您可能正在考虑使用 ipcRenderer 向主进程发送消息,然后在主进程中使用 ipcMain.on 访问它。
所以在你的预加载脚本中:
ipcRenderer.send('send-questions', {data: questions});
在你的主要过程中:
ipcMain.on('send-questions', (msg) => {
console.log(msg.data);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.