我正在开发一个电子应用程序,并尝试使用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>
发布于 2020-07-30 01:50:28
ipcMain.on('launch', () => {
function launch() {
alert('launched!');
}
launch() ;
})
我想你忘了调用launch(),或者你可以这样做
ipcMain.on('launch', () => {
alert('launched!');
})
https://stackoverflow.com/questions/63113734
复制相似问题