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

查看:30
本文介绍了电子: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,转到 Console 视图,然后单击

元素.您应该看到 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>

好处

  • 使用相同的代码同时适用于浏览器和电子
  • 修复了所有 3rd 方库(不仅仅是 jQuery)的问题,而无需指定每个库
  • 脚本构建/打包友好(即 Grunt/Gulp 所有脚本到 vendor.js 中)
  • 不需要 node-integration 为假

来源这里

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

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