我正在做我的第一个电子应用程序-使用Git中的电子快速启动代码。 我无法将事件从preload.js发送到main.js
我成功地将邮件从main.js发送到preload.js-使用 mainWindow中的mainWindow.webContents.send和preload.js中的ipcRenderer.on。 相反,我在这样做时遇到了麻烦。
请注意,调整大小事件可以正常工作,并且可以在main.js中正常接收
preload.js:
const {ipcRenderer} = require('electron')
window.addEventListener('DOMContentLoaded', () => {
// Wait for button click to send event
const button = document.getElementById('clickme');
button.addEventListener('click', (event) =>{
console.log('Preload.Click')
// Send Async - Trial #1 - NOT WORKING !!!!!
ipcRenderer.send('asynchronous-message', 'ping')
})
console.log("preload.js DOMContentLoaded");
})
main.js:
const {app, BrowserWindow, dialog} = require('electron')
const path = require('path')
app.on('ready', createWindow)
// Never gets Called - WHY !!!!
app.on('asynchronous-message', (event, arg) => {
console.log('asynchronous-message received ')
// event.sender.send('asynchronous-reply', 'pong')
})
let mainWindow
console.log("\nmain.js starts");
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true
}
})
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools()
mainWindow.on('closed', function () {
mainWindow = null
})
console.log("listen mainWindow.on('resize') >> works");
mainWindow.on('resize', (event, text) => {
console.log("resize ", mainWindow.getSize());
});
} // createWindow
I was expecting the ipcRenderer.send in preload.js to trigger the event 'on' in main.js displaying the message: 'asynchronous-message received'. - Never happens.
I tried sendSync - it hangs the preload.js program.
What am i doing wrong ?
Thanks
答案 0 :(得分:2)
是的,因为需要监听useEffect(() => {
fetchData();
}, [url]);
事件的ipcMain
不是asynchronous-message
!
您的main.js应该是这样的:
app