【问题标题】:require() and $ not defined in electronrequire() 和 $ 未在电子中定义
【发布时间】:2020-02-03 01:07:55
【问题描述】:

我添加了一段代码来在电子应用程序的 html 文件中添加 jquery 代码。但是每次在调试控制台部分启动应用程序时,它都会显示 require() 并且 $ 未定义。这个问题的解决方法是什么?

image of problem section

你好世界

<button>click</button>

<script>
    window.$ = window.jQuery = require('./node_modules/jquery/dist/jquery.js');
</script>

<script>
    $(document).ready(function() {
        $("button").click(function() {
            $("h1").css("color", "red");
        });
    });
</script>

【问题讨论】:

  • 没有名为require()的javascript函数你是在混合php和JS吗?
  • 对不起,丹。但是在 require() 中可以在任何 node js 包中获取不同的模块
  • 您好,如果我的回答对您有帮助,请随时点赞并接受:)

标签: javascript jquery html electron


【解决方案1】:

要使用 Node.js 的 require 功能,您必须在 BrowserWindow 选项中启用 webPreferences.nodeIntegration

例子:

new BrowserWindow({
    webPreferences: {
        nodeIntegration: true,
    },
    // Other options here.
});

All BrowserWindow options。搜索nodeIntegration

nodeIntegration 以前默认为true。但在 v5.0.0 中,他们将默认设置更改为 false

Link to issue

【讨论】:

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