【问题标题】:enableRemoteModule is missing from Electron v14 TypeScript type definitionsElectron v14 TypeScript 类型定义中缺少 enableRemoteModule
【发布时间】:2021-11-02 15:58:35
【问题描述】:

我已升级到 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 中的错误还是有意更改?有什么解决方法?

【问题讨论】:

    标签: typescript electron


    【解决方案1】:

    现在 Electron 14.0.1 已经发布,这就是我可以为 Main 和 Renderer 进程启用 remote 模块的方法(您的 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 是 remote object,即在 Main 进程中创建的 BrowserWindow 对象的 Renderer 代理。

    【讨论】:

    • 谢谢,在我的情况下,单线可以解决问题
    • 希望我能再次投票。我爬过太多陈旧的线程来达到这个目的
    猜你喜欢
    • 2016-08-31
    • 2015-12-02
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2012-10-20
    • 2017-11-10
    相关资源
    最近更新 更多