Electron v14 TypeScript 类型定义中缺少 enableRemoteModule [英] enableRemoteModule is missing from Electron v14 TypeScript type definitions
问题描述
我已经升级到 Electron 14,重构了我的项目以适应 已删除:远程模块" 重大更改,但由于以下 TypeScript 错误,我无法编译它:
I've upgraded to Electron 14, refactored my project to accomodate for the "Removed: remote module" breaking change, but I'm unable to compile it due to the following TypeScript error:
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 中的错误还是有意更改?有什么解决方法?
Is this a bug or an intentional change in Electron v14? What's a workaround?
推荐答案
现在 Electron 14.0.1 已经发布,这就是我可以为 Main 和 Renderer 进程启用 remote
模块的方法(你的 webPreferences
设置可能会有所不同).
Now Electron 14.0.1 is out, and this is how I could enable remote
modules for both Main and Renderer processes (your webPreferences
settings may vary).
首先,安装@electron/remote
包(重要:不要--save-dev
,因为需要捆绑):
First, install @electron/remote
package (important: no --save-dev
, as it needs to be bundled):
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
对象的 Renderer 代理.
It's important to note, if created from a Renderer process like that, BrowserWindow
is a remote object, i.e. a Renderer proxy to a BrowserWindow
object created inside the Main process.
这篇关于Electron v14 TypeScript 类型定义中缺少 enableRemoteModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!