例子
html页面添加事件
<ul><li><button onclick&#61;"openNewWindow()">new BrowserWindowbutton>li><li><button onclick&#61;"windowOpen()">window.openbutton>li><li>
切换页面 和 打开窗口的方法
window.location.href
切换窗口内容window.open
打开新的窗口new BrowserWindow
打开新的窗口
const path &#61; require(&#39;path&#39;)
function changePage (pageUrl) {window.location.href &#61; pageUrl
}
function windowOpen () {let url &#61; path.join("file://",__dirname,"./pages/introduce.html");let winObj &#61; window.open(url)console.log("winObj : " &#43; winObj)
}
function openNewWindow() {console.log(__dirname);const modalPath &#61; path.join("file://",__dirname,"./pages/introduce.html");let win &#61; new BrowserWindow({ width: 400, height: 275 });win.on("resize", updateReply);win.on("move", updateReply);win.on("close", function() {win &#61; null;});win.loadURL(modalPath);win.show();function updateReply() {const manageWindowBtn &#61; document.getElementById("infoContainer");const message &#61; &#96;Size: ${win.getSize()} Position: ${win.getPosition()}&#96;;manageWindowBtn.innerHTML &#61; message;}
}
如果要使用 require(‘path’), 则需要开启nodejs环境&#xff0c;否则无法使用
打开新窗口需要 —— 开启nodejs环境
const { app, BrowserWindow, BrowserView, globalShortcut } &#61; require(&#39;electron&#39;)
var mainWindow &#61; null app.on(&#39;ready&#39;, () &#61;> {mainWindow &#61; new BrowserWindow({width: 800,height: 800,frame: false, webPreferences: {nodeIntegration: true, enableRemoteModule: true }})mainWindow.loadFile(&#39;./src/index.html&#39;)mainWindow.webContents.openDevTools()mainWindow.on(&#39;closed&#39;, () &#61;> {mainWindow &#61; null})
})
-
设置开启nodejs环境
webPreferences.nodeIntegration: true
-
enableRemoteModule保证renderer.js可以可以正常require(‘electron’).remote
webPreferences.enableRemoteModule: true
-
设置窗口是没有边框的
frame: false, // 是否有边框