我正在开发一个 Electron 应用程序,并尝试使用ipc通讯来调用index.js文件中包含身份验证系统的函数。我需要在ipcMain.on("login", (event, data) => {})
事件中包含此功能。但是可悲的是,函数launch
没有起作用,并且我在控制台中没有任何错误。因此,我认为有另一种方式可以做到这一点。任何帮助将不胜感激
我的index.js文件:
const { app, BrowserWindow, ipcMain } = require('electron')
function createWindow() {
// Create the browser window.
var mainWindow = new BrowserWindow({
title: "AstroLauncher",
width: 1000,
height: 660,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.setMenuBarVisibility(false)
mainWindow.loadFile("home.html");
return mainWindow;
}
app.whenReady().then(() => {
const mainWindow = createWindow()
app.on('activate', function () {
if(BrowserWindow.getAllWindows.length === 0) createWindow()
})
app.on('window-all-closed', function () {
if(process.platform !== 'darwin') app.quit()
})
ipcMain.on("login", (event, data) => {
Authenticator.getAuth(data.u, data.p).then(() => {
event.sender.send('done')
mainWindow.loadFile("home.html");
ipcMain.on('launch', () => {
function launch() {
alert('launched!');
}
})
}).catch((err) => {
event.sender.send("err", { er: err })
})
})
}).catch(err => console.log(err))
和我的home.html文件:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">.
<script src="./index.js"></script>
</head>
<body>
<input type="button" value="Launch!" id="lanBTN" >
<script>
const ipc = require('electron').ipcRenderer
document.querySelector('#lanBTN').addEventListener("click", () => {
ipc.send('launch')
})
</script>
</body>
</html>
最佳答案
ipcMain.on('launch', () => {
function launch() {
alert('launched!');
}
launch() ;
})
我认为您忘记了调用launch()还是可以这样 ipcMain.on('launch', () => {
alert('launched!');
})
关于javascript - Electron ipc通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63113734/