如何在Electron里面使用JQuery

在Electron中直接使用JQuery时,会遇到如下问题

Uncaught ReferenceError : $ is not defined
# or
Uncaught ReferenceError : jQuery is not defined

jQuery isn't defined (globally in the window) because "module" is defined, therefore you can't access the jQuery variable as it doesn't exists really, this problem is caused by the following if statement in the library :

if ( typeof module === "object" && typeof module.exports === "object" ) {
  // set jQuery in `module`
} else {
  // set jQuery in `window`
}

Remember that Electron uses node to work, therefore jQuery has a conflict in the declaration due to module, the no-recommended solution is to set the node-integration property to false in BrowserWindow(), however that would remove the use of node in your app and nobody wants that ... i think.

NPM 引入JQuery修复方式

npm install jquery --save
<script>window.$ = window.jQuery = require('jquery');</script>

Fix with jQuery library file

<!-- If the require doesn't work, include first the jQuery file
<script src="jquery-3.0.0.min.js"></script>-->
<script>window.$ = window.jQuery = require('./jquery-3.0.0.min.js');</script>

https://ourcodeworld.com/articles/read/202/how-to-include-and-use-jquery-in-electron-framework

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容