【问题标题】:Require() is not defined in electron - node jsRequire() 未在电子中定义 - 节点 js
【发布时间】:2019-11-03 20:33:17
【问题描述】:

我正在将电子与节点 js 一起使用。我已经在两个 html 文件中使用过 require(),如下所示:

<script>
        window.$ = window.jQuery = require("jquery");
</script>

现在,当我尝试在同一个项目的另一个 html 文件中执行相同操作时,页面加载时出现“ReferenceError: require is not defined”。这个其他页面显示在 iframe 中,但我不确定这是否重要。

作为参考,我在 main.js 中将 nodeIntegration 设置为 true。

webPreferences: {
  preload: path.join(__dirname, 'preload.js'),
  nodeIntegration: true
}

为什么它允许我在两个 html 文件的脚本中使用 require() 而不是这个新文件?

【问题讨论】:

标签: node.js electron


【解决方案1】:

正如 zero298 提到的,electron 建议使用 webview,我将来可能会使用它。

现在我最终使用 window.parent 来访问我在父窗口中需要的元素,而不使用 jQuery:

var but = window.parent.document.getElementById('saveAction');

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2020-02-03
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    相关资源
    最近更新 更多