Electron:未定义 jQuery [英] Electron: jQuery is not defined

查看:28
本文介绍了Electron:未定义 jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Problem: while developing using Electron, when you try to use any JS plugin that requires jQuery, the plugin doesn't find jQuery, even if you load in the correct path using script tags.

例如,

<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,进入控制台视图,然后单击 <p> 元素.你应该看到 function $ is not defined 或类似的东西.

Running this code above wouldn't work. In fact, open up DevTools, go to the Console view, and click on the <p> element. You should see that function $ is not defined or something to that effect.

推荐答案

更好更通用的解决方案 IMO:

<!-- Insert this line above script imports  -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

<!-- normal script imports etc  -->
<script src="scripts/jquery.min.js"></script>    
<script src="scripts/vendor.js"></script>    

<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>

好处

  • 浏览器和电子都使用相同的代码
  • 修复所有第 3 方库(不仅仅是 jQuery)的问题,而无需指定每个库
  • Script Build/Pack Friendly(即 Grunt/Gulp 所有脚本到 vendor.js)
  • 不要求 node-integration 为假

来源这里

这篇关于Electron:未定义 jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆