1.下载remote:
cnpm install -D @electron/remote
2.主进程中添加代码:
var electron = require('electron');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var win = null;
app.on('ready', function() {
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule:true // 添加语句
}
});
win.loadFile('index.html');
win.on('closed', function() {
win = null;
})
require('@electron/remote/main').initialize() //添加语句
require('@electron/remote/main').enable(win.webContents) //添加语句
})
app.on('window-all-closed', function() {
app.quit();
})
3.渲染进程中使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>窗口标题</title>
</head>
<body>
<button id="openDevToolsBtn">打开开发者工具</button>
</body>
<script>
let remote = require('@electron/remote'); // **使用这种方法引用remote**
document.querySelector('#openDevToolsBtn').addEventListener('click', function () {
remote.getCurrentWindow().webContents.openDevTools();
})
</script>
</html>