【问题标题】:electron remote is undefined with enableremotemodule = true电子遥控器未使用 enableremotemodule = true 定义
【发布时间】:2021-09-09 10:57:59
【问题描述】:

我正在尝试将对话框与 "electron": "^13.1.4" 一起使用,但出现错误 Uncaught TypeError: Cannot read property 'dialog' of undefined. ,即使我设置了 enableremotemodule = true

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
......
......

mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
        contextIsolation: false,
        enableremotemodule: true,
        nodeIntegration: true,
    }
});
    

这是调用电子遥控器但未定义的代码

import { OpenDialogOptions, remote } from 'electron';
.......
.......
openFile() {
    let options: OpenDialogOptions = {};
    console.log(remote); // log undefined
    remote.dialog.showOpenDialog(options).then((filePath) => {
        console.log(filePath);
    });
}

【问题讨论】:

    标签: typescript electron


    【解决方案1】:

    什么是OpenDialogOptions 电子文档中没有项目 试试

    const { dialog } = require('electron').remote

    并将 main.js 中的大小写更改为 enableRemoteModule: true

    请参考Dialog documentation by electron

    【讨论】:

    • OpenDialogOptions是electron.d.ts中定义的接口,如上main.js中已经设置了enableRemoteModule: true。
    【解决方案2】:

    如果您使用的是 14.0 或更高版本,remote 不再是核心电子的一部分,但您可以包含

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

    您还应该在主进程中初始化远程并允许您的窗口远程执行

    在您的主进程中执行此操作

    require("@electron/remote/main").initialize();
    const mainRemote = require("@electron/remote/main");
    mainRemote.enable(win.webContents);
    

    在您的渲染器进程中

    const remote = window.require("@electron/remote");
    const { getCurrentWebContents, getCurrentWindow, dialog } = remote;
    
    const webContents = getCurrentWebContents();
    const currentWindow = getCurrentWindow();
    
    //now show your dialog
       const response = dialog.showMessageBoxSync(currentWindow, {
            buttons: ["Yes", "No"],
            message: "Do you really want to save?",
            title: "Confirm order"
          });
    

    注意:enableRemoteModule 不再适用于电子版 >= 14.0

    Reference

    【讨论】:

    • 相关问题:你知道为什么像import remote from '@electron/remote' 这样的东西不正常吗?我无法让它与我的 vue 组件很好地配合
    猜你喜欢
    • 1970-01-01
    • 2020-09-30
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2018-02-08
    • 2014-10-28
    相关资源
    最近更新 更多