typescript - Electron v14 TypeScript 类型定义中缺少 enableRemoteModule
问题描述
我已经升级到 Electron 14,重构了我的项目以适应“已删除:远程模块”的重大更改,但由于以下 TypeScript 错误,我无法编译它:
Type '{ plugins: true; nodeIntegration: true; contextIsolation: false; enableRemoteModule: true; backgroundThrottling: false; webSecurity: false; }' is not assignable to type 'WebPreferences'.
Object literal may only specify known properties, and 'enableRemoteModule' does not exist in type 'WebPreferences'.ts(2322)
electron.d.ts(12612, 5): The expected type comes from property 'webPreferences' which is declared here on type 'BrowserWindowConstructorOptions'
受影响的代码:
const window = new electron.BrowserWindow({
// ...
webPreferences: {
plugins: true,
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
backgroundThrottling: false,
webSecurity: false
},
// ...
});
这是 Electron v14 中的错误还是有意更改?什么是解决方法?
解决方案
现在 Electron 14.0.1 已经发布,这就是我可以remote
为 Main 和 Renderer 进程启用模块的方式(您的webPreferences
设置可能会有所不同)。
首先,安装@electron/remote
包(重要:否--save-dev
,因为它需要捆绑):
npm install "@electron/remote"@latest
然后,对于主进程:
// from Main process
import * as electron from 'electron';
import * as remoteMain from '@electron/remote/main';
remoteMain.initialize();
// ...
const window = new electron.BrowserWindow({
webPreferences: {
plugins: true,
nodeIntegration: true,
contextIsolation: false,
backgroundThrottling: false,
nativeWindowOpen: false,
webSecurity: false
}
// ...
});
remoteMain.enable(window.webContents);
对于渲染器进程:
// from Renderer process
import * as remote from '@electron/remote';
const window = new remote.BrowserWindow({
webPreferences: {
plugins: true,
nodeIntegration: true,
contextIsolation: false,
backgroundThrottling: false,
nativeWindowOpen: false,
webSecurity: false
}
// ...
});
// ...
// note we call `require` on `remote` here
const remoteMain = remote.require("@electron/remote/main");
remoteMain.enable(window.webContents);
或者,作为单行:
require("@electron/remote").require("@electron/remote/main").enable(window.webContents);
重要的是要注意,如果从这样的 Renderer 进程创建,它BrowserWindow
是一个远程对象,即BrowserWindow
在 Main 进程中创建的对象的 Renderer 代理。
推荐阅读
- curl - curl cron 进程有时无法完成(随机)-Azure Linux
- git - 哪个工作流程会导致 Git 中的“由他们添加”冲突?
- javascript - 如何使用刷新令牌刷新不和谐 Oauth2 访问令牌?
- docker - dockerfile中的phpmyadmin错误如何解决?
- python - 如何在 Python 中更改列表中的项目?
- c# - Unity如何正确获取字符宽度
- python - 如果我们将整数作为输入,为什么 set 中的 pop 函数会弹出第一个元素?
- firebase - 通过 cmd $flutter pub add firebase_auth 添加 firesbase 时,文件 generated_plugin_registrant.dart 显示错误(红色)
- python - 创建唯一组名的请求
- ssas - 在表格立方体模型的 MDX 查询中使用计算成员中的日期范围