我知道有很多已知的解决方案。但没有一个能奏效。我不断收到此错误:

Uncaught Exception:
ReferenceError: $ is not defined
    at: .....

请注意,我正在使用jQuery的npm安装(npm install jquery --save)。

我尝试这样做:
<script>
    window.nodeRequire = require;
    delete window.require;
    delete window.exports;
    delete window.module;
    window.$ = window.jQuery = require('jquery');
</script>

但仍然出现错误。

然后我尝试了:
<script>window.$ = window.jQuery = require('jquery');</script>

但这也没有用。

我也尝试使用本地文件:
<script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js');</script>

但是,再次出现可怕的“未定义$”错误。
有人可以解释发生了什么吗?我知道这些解决方案应该如何工作,但我不明白为什么它们对我不起作用。

这是我当前状态下的完整HTML页面:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>...</title>
  </head>
  <body>
    <h1>Test</h1>
    <div id = "test"></div>
    <script>window.$ = window.jQuery = require('./jquery-3.3.1.min.js');
   </script>

    <script>
      require('./renderer.js')
    </script>

  </body>
</html>

我从https://github.com/electron/electron-quick-start的 Electron 快速入门开始了这个项目。

提前致谢。

编辑:有趣的是,oauth库也干扰了jquery。但是即使删除了oauth,它仍然被破坏了。

最佳答案

我还在我的Electron项目中完成了npm install jquery --save

我使用jQuery的方法是要求在我的HTML <head>标签中使用它,然后再在其他任何<script>标签中添加它。

<!DOCTYPE html>
<html lang="en">
  <head>
    ...
    <!-- jQuery (Should be declared before other JS imports) -->
    <script>
      var $ = jQuery = require("jquery")
    </script>
    ...
    <script src="../node_modules/jquery/dist/jquery.slim.min.js"></script>
    <script src="../node_modules/popper.js/dist/umd/popper.min.js"></script>
    <script src="../node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    ...
  </head>
  <body>
    ....
  </body>
</html>

我需要安装jQuery for Bootstrap,因此在声明jQuery脚本源导入之前需要它,然后是我需要的其他JS。

编辑集成electron-forge 后,上述解决方案不再对我有效。起作用的是这里提到的解决方案:https://stackoverflow.com/a/37480521/1392578

10-04 22:20
查看更多