问题:在使用Electron进行开发时,当你尝试使用任何需要jQuery的JS插件时,插件都找不到jQuery,即使你使用脚本标签加载在正确的路径中。

例如,

<body>
<p id="click-me">Click me!</p>
...
<script src="node_modules/jquery/dist/jquery.min.js"></script> //jQuery should be loaded now
<script>$("#click-me").click(() => {alert("Clicked")});</script>
</body>

运行上面的代码是行不通的。实际上,打开DevTools,转到Console视图,然后单击<p>元素。你应该看到函数$没有定义或者类似的效果。


当前回答

2022年。这对我很管用。

安装JQuery

npm install jquery --save

使用script标签将jquery添加到index.html文件中。 src应该是你的node_modules文件夹中jquery的路径

    <script type="text/javascript" src="../node_modules/jquery/dist/jquery.min.js"></script>

现在JQuery将可用/定义为$在你的渲染器进程(renderer.js文件)

其他回答

1.使用npm安装jQuery。

npm install jquery --save

2.

<!--firstly try to load jquery as browser-->
<script src="./jquery-3.3.1.min.js"></script>
<!--if first not work. load using require()-->
<script>
  if (typeof jQuery == "undefined"){window.$ = window.jQuery = require('jquery');}
</script>

一个漂亮干净的解决方案

使用npm安装jQuery。(npm安装jquery——保存) 使用它:<script> let $ = require("jquery") </script>

只需使用以下命令安装Jquery。

npm install --save jquery

之后,请把下面的行在js文件,你想使用Jquery

let $ = require('jquery')

2022年。这对我很管用。

安装JQuery

npm install jquery --save

使用script标签将jquery添加到index.html文件中。 src应该是你的node_modules文件夹中jquery的路径

    <script type="text/javascript" src="../node_modules/jquery/dist/jquery.min.js"></script>

现在JQuery将可用/定义为$在你的渲染器进程(renderer.js文件)

如果你使用的是Angular2,你可以用下面的代码创建一个新的js文件:

// jquery-electron.js

if ((!window.jQuery || !window.$) && (!!module && !!module.exports)) {
      window.jQuery = window.$ = module.exports;
}

然后把它放在。angular-cli.json的jquery路径后面:

"scripts": [
    "../node_modules/jquery/dist/jquery.js",
    "assets/js/jquery-electron.js",
    ...
    ...
]