美文网首页
如何在Electron里面使用JQuery

如何在Electron里面使用JQuery

作者: 时见疏星 | 来源:发表于2018-03-02 10:36 被阅读1222次

在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

相关文章

网友评论

      本文标题:如何在Electron里面使用JQuery

      本文链接:https://www.haomeiwen.com/subject/synoxftx.html