enableRemoteModuleremote @electron/remotenpm
npm install --save @electron/remote
复制代码
主进程初始化开启mainWindowenableRemoteModule
require('@electron/remote/main').initialize()
require("@electron/remote/main").enable(mainWindow.webContents)
复制代码
渲染进程
const {BrowserWindow}=require('@electron/remote')
复制代码
- 完整代码
主进程:
const { app, BrowserWindow} = require('electron')
app.on('ready', () => {
let mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
})
require('@electron/remote/main').initialize()
require("@electron/remote/main").enable(mainWindow.webContents)
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools();
})
复制代码
渲染进程:
const {BrowserWindow}=require('@electron/remote')
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('node-version').innerHTML = process.versions.node
document.getElementById('send').addEventListener('click', () => {
let win = new BrowserWindow({
width: 800,
height: 600,
})
win.loadURL('https://www.baidu.com')
})
})
复制代码