nos*_*tio 20 typescript electron
我已经升级到 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'
Run Code Online (Sandbox Code Playgroud)
受影响的代码:
const window = new electron.BrowserWindow({
// ...
webPreferences: {
plugins: true,
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
backgroundThrottling: false,
webSecurity: false
},
// ...
});
Run Code Online (Sandbox Code Playgroud)
这是 Electron v14 中的错误还是有意更改?有什么解决方法吗?
nos*_*tio 50
remote现在 Electron 14.0.1 已经发布,这就是我为主进程和渲染进程启用模块的方法(您的webPreferences设置可能会有所不同)。
首先,安装@electron/remote包(重要:不--save-dev,因为它需要捆绑):
npm install "@electron/remote"@latest
Run Code Online (Sandbox Code Playgroud)
然后,对于主进程:
// 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);
Run Code Online (Sandbox Code Playgroud)
对于渲染器进程:
// 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);
Run Code Online (Sandbox Code Playgroud)
或者,作为一句:
require("@electron/remote").require("@electron/remote/main").enable(window.webContents);
Run Code Online (Sandbox Code Playgroud)
需要注意的是,如果从这样的渲染器进程创建,则它BrowserWindow是一个远程对象BrowserWindow,即主进程内创建的对象的渲染器代理。
| 归档时间: |
|
| 查看次数: |
12084 次 |
| 最近记录: |