引入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

posted @ 2015-11-30 15:02  若强  Views(3806)  Comments(0Edit  收藏  举报