精华 Electron14以后如何使用remote模块

发布于 2 年前 作者 phonegap100 1238 次浏览 最后一次编辑是 2 年前 来自 分享

Electron remote模块在不同版本的electorn中配置方法不一样,但是使用方法都是一样的。

1、在electron10.x以前我们可以直接在渲染进程中使用remote模块

如下:

const { remote } = require("electron");
const BrowserWindow=remote.BrowserWindow

2、在electron10以后electron14之前我们要使用remote模块需要在主进程进行配置 enableRemoteModule:true

代码如下: main.js

 const mainWindow = new BrowserWindow({
     width: 800,
     height: 600,
     webPreferences:{
         nodeIntegration:true,  //开启渲染进程中使用nodejs
         contextIsolation:false, //开启渲染进程中使用nodejs    In Electron 12, the default will bechanged to `true 
         enableRemoteModule:true  //启用Remote模块
     }
 });

3、Electron14以后要使用remote模块我们还需要单独安装引入:

1、安装@electron/remote

npm install --save @electron/remote 或者 cnpm install --save @electron/remote 或者 yarn add @electron/remote

2、主进程配置下面几行代码

const remote=require(’@electron/remote/main’) remote.initialize() remote.enable(mainWindow.webContents);

主进程配置详细代码

const remote=require(’@electron/remote/main’) remote.initialize() … const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences:{ nodeIntegration:true, //开启渲染进程中使用nodejs contextIsolation:false, //开启渲染进程中使用nodejs In Electron 12, the default will bechanged to `true } }); //启用Remote模块 remote.enable(mainWindow.webContents);

3、渲染进程引入remote模块

const { BrowserWindow } = require("@electron/remote")

回到顶部