引入RequireJS(一)


例子

http://www.utlcenter.com/user/index.aspx

image

一、文件引用

<script src="Js/require.js" defer async="true" data-main="Js/main.js"></script>

async屬性表明這個文件需要異步加載,避免網頁失去響應。IE不支持這個屬性,只支持defer,所以把defer也寫上。

data-main屬性的作用是,指定網頁程序的主模塊

二、主模塊依賴main.js

假定主模塊依賴jquery、Statistics和GetStatistics這三個模塊,main.js就可以這樣寫

require.config({

paths: {

"jquery": "jquery-1.9.1.min",

"Statistics": "SiteJs/Statistics",

"GetStatistics": "SiteJs/GetStatistics"

}

});

requirejs(['jquery', 'Statistics', 'GetStatistics'],

function () {

//jQuery, canvas and the app/sub module are all

//loaded and can be used here now.

//var _GetTotalMoney = GetTotalMoney();

//$(".Js_cal").html(_GetTotalMoney);

});

 

三、AMD模塊的寫法

require.js加載的模塊,采用AMD規范。也就是說,模塊必須按照AMD的規定來寫。

采用特定的define()函數來定義

四、加載非規范的模塊

 

理論上,require.js加載的模塊,必須是按照AMD規范、用define()函數定義的模塊。但是實際上,雖然已經有一部分流行的函數庫(比如jQuery)符合AMD規范,更多的庫並不符合。

參考鏈接

http://www.ruanyifeng.com/blog/2012/11/require_js.html

http://www.requirejs.cn/

http://www.cnblogs.com/snandy/archive/2012/05/22/2513652.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM