在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