最近在了解Electron框架寫(xiě)應(yīng)用,偶然發(fā)現(xiàn)在html中使用<script src="./jquery.js"></script>這種方式引入JQuery,明明引入了,程序中卻始終報(bào)錯(cuò)顯示:Uncaught ReferenceError: $ is not defined。
創(chuàng)新互聯(lián)于2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元龍沙做網(wǎng)站,已為上家服務(wù),為龍沙各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
經(jīng)查閱資料,發(fā)現(xiàn)不僅JQuery不能用,RequireJS、Meteor、AngularJS也不能使用。
原因是Electron 在運(yùn)行環(huán)境中引入了 Node.js,默認(rèn)啟用了Node.js的require模塊,而這些框架為了支持commondJS標(biāo)準(zhǔn),當(dāng)Window中存在require時(shí),會(huì)啟用模塊引入的方式。所以在 DOM 中有一些額外的變量,比如module、exports和require。這導(dǎo)致了許多庫(kù)不能正常運(yùn)行,因?yàn)樗鼈円残枰獙⑼淖兞考尤脒\(yùn)行環(huán)境中。
解決方法有很多,官方的解決方法是1、2:
1、禁用Node.js:若不使用Node.js則通過(guò)在主進(jìn)程中禁用Node.js來(lái)解決。
// 在主進(jìn)程中
var mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: false
}
});
2、變量重命名:假如依然需要使用 Node.js 和 Electron 提供的 API,則需要在引入那些庫(kù)之前將這些變量重命名。
<head>
<script>
// 重命名 Electron 提供的 require
window.nodeRequire = require;
delete window.require;
delete window.exports;
delete window.module;
</script>
<script type="text/javascript" src="jquery.js"></script>
</head>
這樣,就可以用nodeRequire來(lái)使用node模塊,又能使用jQUery了,但這種方法不適合web項(xiàng)目,在web里將無(wú)法正常瀏覽。
3、采用Node.js的require引入:(有些時(shí)候不要帶后綴名.js)
<script>window.$ = window.jQuery = require("./js/jquery.min");</script>
4、自定義$、jQuery變量
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script>if (typeof module === 'object') {window.jQuery = window.$ = module.exports;};</script>
5、
<!-- 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>
6、去掉框架中的模塊引入判斷代碼:針對(duì)JQuery
將JQuery中的如何代碼
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}
改為
!function(a,b){b(a)}
我個(gè)人還是比較喜歡使用第三種,簡(jiǎn)單代碼少
文章標(biāo)題:折騰Electron對(duì)JQuery的支持
瀏覽路徑:http://chinadenli.net/article36/gpcdpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、微信小程序、網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷(xiāo)型網(wǎng)站建設(shè)知識(shí)