【问题标题】:TypeError: Cannot read property 'showMessageBox' of undefined electron in node jsTypeError:无法读取节点 js 中未定义电子的属性“showMessageBox”
【发布时间】:2019-09-09 16:30:45
【问题描述】:

以下代码只是使用电子节点模块显示对话框。

app.js

    const { dialog } = require('electron')
    const response = dialog.showMessageBox(null);
    console.log(response);

需要帮助以了解为什么我收到以下错误消息:

    const response = dialog.showMessageBox(null);
                            ^
    TypeError: Cannot read property 'showMessageBox' of undefined
        at Object.<anonymous> (C:\Users\1217688\Desktop\WebApp\node-elect-test\app.js:2:25)
        at Module._compile (module.js:660:30)
        at Object.Module._extensions..js (module.js:671:10)
        at Module.load (module.js:573:32)
        at tryModuleLoad (module.js:513:12)
        at Function.Module._load (module.js:505:3)
        at Function.Module.runMain (module.js:701:10)
        at startup (bootstrap_node.js:190:16)
        at bootstrap_node.js:662:3

【问题讨论】:

  • 发布你的 main.js 文件?
  • app.js - 这是我的主文件
  • 你的 package.json 中是否有电子,你是否运行过npm install
  • 是的,我确实安装了电子 npm 模块

标签: node.js express electron


【解决方案1】:

在渲染器进程中,添加.remote

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

【讨论】:

    【解决方案2】:

    在应用准备就绪时尝试调用对话框模块:

    app.on('ready', () => {
      const { dialog } = require('electron')
      dialog.showMessageBox(null);
    })
    

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 2018-11-30
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      相关资源
      最近更新 更多