requirejs学习之-- 初始化(一)
為了規(guī)范在項(xiàng)目中使用的javascript代碼,我們使用了requirejs框架。
初始階段,我們?cè)诎粹o的點(diǎn)擊事件中調(diào)用創(chuàng)建的模塊,代碼如下:
function button_click() {_this = this;var args = _this["Command_Params"] || (_this.config || {})["Command_Params"];var cmd = _this["Command"];if (cmd) {cmd.onclick?cmd.onclick(args):"";} else {var url = _this["Command_Url"] || (_this.config || {})["Command_Url"];if (!url) return;require([url], function(Class) {var cmd = new Class(_this, args);cmd.onclick(args);_this["Command"] = _this["Command_Singled"] || (_this.config || {})["Command_Singled"] == false ? null : cmd;});} }在這里我們?cè)诎粹o的屬性中添加了Command_Url來(lái)標(biāo)識(shí)模塊地址,監(jiān)聽(tīng)了按鈕的點(diǎn)擊事件,在點(diǎn)擊的時(shí)候引用按鈕中配置的模塊并執(zhí)行onclick方法。不得不說(shuō)這是一種很簡(jiǎn)便的使用模塊的方式,但是不是嚴(yán)格的AMD模式。
后來(lái)在使用的過(guò)程中突然想在頁(yè)面初始化的時(shí)候引用一個(gè)模塊,直接使用require方法就出現(xiàn)了問(wèn)題:
require(["./plug-in/jtgcmobile/dd/user.js"],function(Class){var user =new Class();user.showSomeThing();user.login();}) require.js:166 Uncaught Error: Mismatched anonymous define() module: function (){return o}糾結(jié)了好長(zhǎng)時(shí)間,發(fā)現(xiàn)這個(gè)方法在控制臺(tái)可以直接運(yùn)行,排除模塊定義錯(cuò)誤、加載代碼錯(cuò)誤。那就剩下一個(gè)問(wèn)題,requirejs的加載模式問(wèn)題導(dǎo)致在頁(yè)面加載的過(guò)程中無(wú)法執(zhí)行require方法。
又去看了一下官方的文檔,文檔上雖然沒(méi)有明說(shuō)不可以這樣使用,但是提供了一個(gè)方法在頁(yè)面初始化的時(shí)候加載模塊。代碼如下:
<script data-main="scripts/main" src="scripts/require.js"></script>也就是說(shuō)在加載requirejs的時(shí)候就要指定初始加載模塊。
按照這種方式加載后一切正常了。
所以 ?data-main 屬性還是比較重要的。
?
轉(zhuǎn)載于:https://www.cnblogs.com/Leechg/p/5736384.html
總結(jié)
以上是生活随笔為你收集整理的requirejs学习之-- 初始化(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Delphi格式化函数Format、Fo
- 下一篇: 4.H - 组合