试图在 Electron 中制作一个按钮,但无论我做什么,它都不起作用。
据我所知,我的代码中的所有内容都应该是正确的,但由于某种原因,当我按下按钮时,终端中没有任何记录。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
const {ipcRenderer} = require('electron');
function shareData() {
ipcRenderer.send("msg", "hello from renderer");
alert("all set")
}
</script>
</head>
<body style="background-color:rgb(43, 43, 43);">
<button onclick="shareData()" >Primary</button>
</body>
</html>
main.js
const { app, BrowserWindow, ipcMain } = require('electron');
ipcMain.on("msg",(event, data)=>{
console.log(data);
});
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
});
win.loadFile('index.html');
};
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});