const {app, BrowserWindow} = require('electron') const path = require('path') let mainWindow function createWindow () { console.log(123) mainWindow = new BrowserWindow({ width: 900, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js'), nodeIntegration:true//設置此項以使用nodejs }, frame:true }) mainWindow.loadFile('main.html') mainWindow.on('closed', function () { mainWindow = null }) } app.on('ready', createWindow) app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) app.on('activate', function () { if (mainWindow === null) createWindow() })
第一次發博:
在函數createWindow中設置第一個渲染進程mainWindow里有一個webpreferences,里面的第一個參數暫不知道,也許后續會更新。第二個參數是如果當前進程所使用的的html文件需要用到nodejs模塊則必須加這個參數,且設置為true,否則所有目標html文件中的nodejs語句都會失效