我是电子新手。我有两个html页面,我想在点击一个按钮时打开第二页。我的代码如下,但我只是得到一个空白的窗口;不是第二页。
这是index.js
const electron = require('electron')
const {app, BrowserWindow} = electron
app.on('ready',()=>{
let win = new BrowserWindow({width:960, hehight:540})
win.loadURL(`file://${__dirname}/login.html`)
})
exports.openWindow = (fileName) => {
let win = new BrowserWindow({width:960, height:540})
win.loadURL(`file://${__dirname}/` + fileName + `.html`)
}
这是login.js
const remote = require('electron').remote
const index = remote.require('./index.js')
var login = document.getElementById('login')
login.addEventListner('click', () => {
var window = remote.getCurrentWindow()
index.openWindow('pageTwo')
window.close()
}, false)
login是html按钮的id。
我想要第二页。我怎么能这样做?
答案 0 :(得分:2)
您可以使用IPCRenderer和IPCMain轻松实现此目的,以便在主进程和渲染器之间传递消息。
index.js
const electron = require('electron')
const {app, BrowserWindow, ipcMain} = electron
app.on('ready',()=>{
let win = new BrowserWindow({width:960, hehight:540})
win.loadURL(`file://${__dirname}/login.html`)
})
ipcMain.on('open-new-window', (event, fileName) => {
let win = new BrowserWindow({width:960, height:540})
win.loadURL(`file://${__dirname}/` + fileName + `.html`)
}
如您所见,我更改了您的代码只是为了添加ipcMain
并从渲染器接收消息。
login.js
const {ipcRenderer} = require('electron');
let login = document.getElementById('login');
login.addEventListner('click', () => {
ipcRenderer.send('open-new-window', 'pageTwo');
}, false);
login.js和ipcRenderer
也一样。
文档解释的方式比我更好,可以找到here for ipcMain和here for ipcRenderer。
答案 1 :(得分:1)
我查看了您的代码,并且从我看到您需要主进程,您无法执行此操作,您需要使用IPC从渲染过程发送消息或使用远程{ {3}}对象并从ID获取窗口并使用loadURL。
使用IPC
index.js
const { ipcMain } = require("electron");
ipcMain.on("changeWindow", function(event, arg) {
switch (arg) {
case "page1":
win.loadURL("Page1 URL");
break;
case "page2":
win.loadURL("Page2 URL");
break;
case "page3":
win.loadURL("Page3 URL");
break;
...
}
});
login.js
const { ipcRenderer } = require("electron");
function onButtonClick() {
ipcRenderer.send("changeWindow", "page2");
}
远程使用BrowserWindow的第二个选项。
login.js
const { BrowserWindow } = require("electron").remote;
function onButtonClick() {
let win = BrowserWindow.fromId("ID of your window");
win.loadURL("URL you want to load (your login.html file)");
}
// OR
function onButtonClick() {
let win = BrowserWindow.getFocusedWindow();
win.loadURL("URL you want to load (your login.html file)");
}
// OR
function onButtonClick() {
/*
Not the best method but would work.
*/
let wins = BrowserWindow.getAllWindows();
let windowIndex = /* index of your window in the wins array */
wins[windowIndex].loadURL("URL you want to load (your login.html file)");
}
答案 2 :(得分:0)
使用来自渲染器进程的远程api访问所需的功能有时会导致在使用参数调用该functionlaity时出现死锁。您应该使用ipc
// main process
const { app, BrowserWindow, ipcMain: ipc } = require("electron");
let win;
app.on("ready", () => {
win = new BrowserWindow();
win.loadURL(`file://${__dirname}/index.html`);
});
ipc.on("send-window-id", (event) => {
event.sender.send("window-id-sent", win.id);
});
// renderer process
<html>
<head></head>
<body>
<button>new window</button>
<script>
const { remote: { BrowserWindow }, ipcRenderer: ipc } = require("electron");
var button = document.querySelector("button");
button.addEventListener("click", event => {
let win = new BrowserWindow();
win.loadURL(`file://${__dirname}/page2.html`);
ipc.send("send-window-id");
});
ipc.on("window-id-sent", (event,id) => {
BrowserWindow.fromId(id).close();
});
</script>
</body>
</html>
BrowserWindow.fromId
接收窗口的id并返回该id