解决 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
    })
})
相关推荐
fishmemory7sec7 分钟前
Electron 主进程与渲染进程、预加载preload.js
前端·javascript·electron
YUELEI1189 分钟前
构建electron项目
electron·vue3
fishmemory7sec10 分钟前
Electron 使⽤ electron-builder 打包应用
前端·javascript·electron
豆豆1 小时前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建
JUNAI_Strive_ving1 小时前
番茄小说逆向爬取
javascript·python
看到请催我学习2 小时前
如何实现两个标签页之间的通信
javascript·css·typescript·node.js·html5
twins35202 小时前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
qiyi.sky2 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~2 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
哪 吒2 小时前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od