1.在background.js主进程中先设置登录窗口的大小
2.创建一个新的js文件,将ipcRenderer引入
const { ipcRenderer } = window.require("electron");
module.exports = { ipcRenderer };
这是将会报错,如下图 所示:
解决方案 :
(1)npm install --save is-electron
(2)在新建的js文件中引入判断条件
const isElectron = require("is-electron");
if (isElectron()) {
const { ipcRenderer } = window.require("electron");
module.exports = { ipcRenderer };
}
3.在登录页面,引入ipcRenderer;登录按钮监听点击事件,进行发送命令
if (isElectron()) {
ipcRenderer.send("setWin", {
width: 820,
height: 640,
});
}
4.background.js文件中使用ipcMain进行监听发送过来的数据,新建窗口
import {ipcMain} from "electron";
ipcMain.on("setWin", (event, res) => {
let win = createdMainWindow(res);})
function createdMainWindow(res) {
let mainWindow = new BrowserWindow({
width: res.width,
height: res.height,
minWidth: 800,
minHeight: 600,
resizable: true,
maximizable: true,
minimizable: true,
webPreferences: {
scrollBounce: false,
nativeWindowOpen: true,
nodeIntegration: true,
contextIsolation: false,
webviewTag: true,
},
frame: false,
backgroundColor: "#100000000",
});
//mainWindow.webContents.openDevTools();
mainWindow.removeMenu();
mainWindow.loadURL("http://localhost:8081/#/home");
return mainWindow;
}
5.这里有一个注意点,因为是新建了一个窗口,所以需要把原先的窗口关闭,也是使用同样的方法.
(1)ipcRenderer发送关闭命令
ipcRenderer.send("close")
(2)ipcMain监听关闭命令
ipcMain.on("close",()=>{win.close()}) //win是要关闭的登录窗口的名称