electron14之后版本使用remote


@electron/remote

安裝

npm install --save @electron/remote
//建議使用yarn
yarn add @electron/remote
注意不要能使用-D,否則會出現調試時候正常使用,打包后報找不到包的情況

使用

主進程當中

require('@electron/remote/main').initialize()
require('@electron/remote/main').enable(mainWindow.webContents);

electron版本>=14.0.0,每個單獨的webContents想要使用remote module,必須使用新的enable API來一個個使能.默認remote module是不可用的
electron版本<14.0.0  版本可以使用enableRemoteModule來控制
webPreferences{enableRemoteModule:false}可以禁用remote module

渲染進程

const { BrowserWindow } = require('@electron/remote')

不使用remote情況下調用electron dialog等信息

使用IPC消息在線程間通信

webview

// main process
app.on("web-contents-created", (e, contents) => {
  if (contents.getType() == "webview") {
    //啟用remote
    require("@electron/remote/main").enable(contents);
    //替換new-window事件
    contents.setWindowOpenHandler((details) => {
      log.log("web-contents new-window", details.url);
      return { action: "allow" };
    });
  }
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM