解决 Electron 14 之后版本使用 remote 模块报错

文章目录

  • [Electron 介绍](#Electron 介绍)
  • [remote 模块介绍](#remote 模块介绍)
  • [Electron 14 之前版本使用 remote 模块](#Electron 14 之前版本使用 remote 模块)
  • [Electron 14 之后版本使用 remote 模块](#Electron 14 之后版本使用 remote 模块)

Electron 介绍

Electron是一个能够使用HTML、CSS和JavaScript构建跨平台桌面应用的开源框架。它是由GitHub开发并维护的,最初是为了构建GitHub的桌面客户端而创建的。

Electron基于Node.js和Chromium项目,通过将这两个技术结合起来,它能够在桌面环境中运行Web技术。而且,它提供了丰富的API和工具,使开发者能够轻松地创建跨平台的桌面应用。

使用Electron,开发者可以使用熟悉的Web技术进行应用开发,无需学习新的编程语言或工具。应用程序可以在Windows、Mac和Linux等多个操作系统上运行,而且具有与原生应用相似的外观和性能。

Electron的应用程序由两部分组成:主进程和渲染进程。主进程是一个Node.js进程,负责管理应用的生命周期和与操作系统的交互,而渲染进程是一个Chromium渲染进程,负责显示应用的用户界面。

除了基本的UI显示功能,Electron还提供了一系列强大的API,用于实现文件系统访问、网络通信、系统通知、菜单、快捷键等功能。开发者还可以使用Electron的插件机制来扩展应用的功能。

总的来说,Electron是一个强大且易于使用的框架,能够让开发者用Web技术构建跨平台的桌面应用。它被广泛应用于各种应用领域,如代码编辑器、图形设计工具、聊天应用、音乐播放器等。

remote 模块介绍

Electron的remote模块是Electron提供的一个功能强大且方便的API,用于在主进程和渲染进程之间进行通信和调用。

在Electron中,主进程和渲染进程是分离的,它们运行在不同的上下文中。主进程负责管理应用的生命周期和与操作系统的交互,而渲染进程则负责显示应用的用户界面。

remote模块允许在渲染进程中访问主进程中的对象和方法。在渲染进程中,可以像访问本地对象一样直接访问主进程中的对象,而无需通过IPC(进程间通信)进行复杂的消息传递。

通过remote模块,开发者可以使用一些预定义的模块,如BrowserWindowappMenu等,直接在渲染进程中调用它们的方法。

需要注意的是,使用remote模块访问主进程中的对象会涉及到一些安全性和性能方面的考虑。因此,在使用remote模块时需要谨慎处理,避免出现潜在的安全漏洞或性能问题。

总的来说,Electron的remote模块提供了一个简单而强大的方式,在主进程和渲染进程之间进行通信和调用。它能够帮助开发者更方便地实现不同进程之间的交互,提高应用的开发效率和用户体验。

Electron 14 之前版本使用 remote 模块

main.js

js 复制代码
// 导入 app, BrowserWindow 模块
const { app, BrowserWindow, ipcMain } = require('electron')

app.on('ready', () => {
  let mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  }) 

  mainWindow.loadFile('index.html')
  mainWindow.webContents.openDevTools()
  ipcMain.on('message', (event, arg) => {
    console.log('arg:' + arg)
    event.reply('reply', 'hello from main process')
  })
})

renderer.js

js 复制代码
/**
 * This file is loaded via the <script> tag in the index.html file and will
 * be executed in the renderer process for that window. No Node.js APIs are
 * available in this process because `nodeIntegration` is turned off and
 * `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
 * to expose Node.js functionality from the main process.
 */

// 引入模块
const { ipcRenderer} = require('electron');
const { BrowserWindow } = require('electron').remote;

window.addEventListener('DOMContentLoaded', () => {
    document.getElementById('node-version').innerHTML = process.versions.node
    document.getElementById('send').addEventListener('click', () => {
        ipcRenderer.send('message', 'hello from renderer')
        let win = new BrowserWindow({ width: 800, height: 600})
        win.loadURL('https://baidu.com')
    })
    ipcRenderer.on('reply', (event, arg) => {
        document.getElementById('message').innerHTML = arg
    })
})

Electron 14 之后版本使用 remote 模块

remote 模块移除,所以需额外安装该模块。

进入项目目录,安装 remote 模块

bash 复制代码
npm install --save @electron/remote

main.js

js 复制代码
// 导入 app, BrowserWindow 模块
const { app, BrowserWindow, ipcMain } = require('electron')

// 第一步:引入remote
const remote = require('@electron/remote/main');
// 第二步: 初始化remote
remote.initialize();

app.on('ready', () => {
 
  let mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  }) 

  mainWindow.loadFile('index.html')
  mainWindow.webContents.openDevTools()
  ipcMain.on('message', (event, arg) => {
    console.log('arg:' + arg)
    event.reply('reply', 'hello from main process')
  })

  // 第三步: 开启remote服务
  remote.enable(mainWindow.webContents);

renderer.js

js 复制代码
/**
 * This file is loaded via the <script> tag in the index.html file and will
 * be executed in the renderer process for that window. No Node.js APIs are
 * available in this process because `nodeIntegration` is turned off and
 * `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
 * to expose Node.js functionality from the main process.
 */

const { ipcRenderer} = require('electron')
// 引入remote远程操作窗口,新版本需要下载@electron/remote
const {BrowserWindow} = require('@electron/remote');

window.addEventListener('DOMContentLoaded', () => {
    document.getElementById('node-version').innerHTML = process.versions.node
    document.getElementById('send').addEventListener('click', () => {
        ipcRenderer.send('message', 'hello from renderer')
        let win = new BrowserWindow({ width: 800, height: 600})
        win.loadURL('https://baidu.com')
    })
    ipcRenderer.on('reply', (event, arg) => {
        document.getElementById('message').innerHTML = arg
    })
})
相关推荐
kyriewen1 小时前
Anthropic 估值逼近万亿美元,Claude Sonnet 5 + Claude Science 一天两连发
前端·ai编程·claude
小徐_23332 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
山河木马4 小时前
矩阵专题3-怎么创建投影矩阵(uProjectionMatrix)
javascript·webgl·计算机图形学
天蓝色的鱼鱼5 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷6 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花6 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷6 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜6 小时前
Spring Boot 核心知识点总结
前端
lichenyang4536 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端