typescript - Electron v14 TypeScript 类型定义中缺少 enableRemoteModule

标签 typescript electron

我已经升级到 Electron 14,重构了我的项目以适应 "Removed: remote module"破坏性更改,但由于以下 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);
重要的是要注意,如果是从这样的渲染器进程创建的,BrowserWindowremote object ,即 BrowserWindow 的渲染器代理在 Main 进程中创建的对象。

关于typescript - Electron v14 TypeScript 类型定义中缺少 enableRemoteModule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69059668/

相关文章:

javascript - Testcafe headless 可见性检查

node.js - 使用带有 s3 的 Electron 更新程序的 Electron 应用程序导致访问被拒绝错误

electron - 用 Electron 嵌入 Mongodb

html - Electron + Ionic - ionic 行高度不会根据其内容进行调整

javascript - Angular - 如何将多个 FormGroups 插入 FormArray?

angular - 为什么我不能将字符串传递给@input

javascript - 如何在Electron-Vue项目中使用多scss

electron - 运行 npx cypress open 时出现 ERR_FAILED (-2) 错误

javascript - TypeScript 在生成的 JavaScript 中列出导入的接口(interface)和类型

javascript - Angular 两次发送 http 请求