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

查看:70
本文介绍了电子:未定义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>

好处

  • 使用相同的代码可同时用于浏览器和电子产品
  • 无需指定每个第三方库即可修复所有第三方库(不仅仅是jQuery)的问题
  • 友好的脚本构建/打包(即,将所有脚本都咕unt咕Gu的/Gulp到vendor.js中)
  • 不需要node-integration为假
  • Works for both browser and electron with the same code
  • Fixes issues for ALL 3rd-party libraries (not just jQuery) without having to specify each one
  • Script Build / Pack Friendly (i.e. Grunt / Gulp all scripts into vendor.js)
  • Does NOT require node-integration to be false

source 此处

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

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