版本升级注意项
每个版本都有废弃和移除的API,废弃的依旧可以用,所以这里只列一些常用的被移除的API及对应的替换方法。
各版本移除的API
官方文档:https://www.electronjs.org/docs/latest/breaking-changes/#removed-remote-module
14.0
remote模块
移除remote模块前最新的Electron版本
1 | "electron": "13.4.0" |
The remote
module was deprecated in Electron 12, and will be removed in Electron 14. It is replaced by the @electron/remote
module.
1 | // Deprecated in Electron 12: |
替换方式
安装依赖
1 | npm install --save @electron/remote |
主进程
1 | // In the main process: |
渲染进程
1 | // Replace with: |
打包找不到依赖的解决方法
https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/guide.html#table-of-contents
项目根目录vue.config.js
中修改配置,没有该文件创建即可。
1 | // vue.config.js |
官方推荐方式
remote
模块官方不推荐使用,如果项目使用remote
的地方较少,可以更换为下面的方式替换。
1 | // Renderer process |
示例
假如弹窗选择路径的功能
原来在渲染进程中是这样的
1 | select_dir: function() { |
替换为
1 | // 渲染进程 |
13.0
Extension
1 | // Removed in Electron 13 |
1 | // Removed in Electron 13 |
1 | // Removed in Electron 13 |
12.0
crashReporter
Removed: crashReporter.getCrashesDirectory()
The crashReporter.getCrashesDirectory
method has been removed. Usage should be replaced by app.getPath('crashDumps')
.
1 | // Removed in Electron 12 |
9.0
<webview>.getWebContents()
This API, which was deprecated in Electron 8.0, is now removed.
1 | // Removed in Electron 9.0 |
错误汇总
require
引用时报错
1 | const { ipcRenderer } = window.require('electron'); |
错误信息
window.require not a function
解决方式
1 | webPreferences: { |