RequireJs入门实例
*
参考文章:requireJs简要介绍和完整例子
按照这篇文章自己动手写了一个例子,做了小修改
一:目录
二:源码
0,index.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Home</title> <style type="text/css"> *{font-family:"微软雅黑","microsoft Yahei",verdana;} .text01{line-height:20px;font-size:13px;font-family:verdana;} pre{margin:0px;padding:2px 0px;font-size:13px;font-family:verdana;} .div01{border:1px solid red;text-align:left;padding:5px; } </style> </head> <body> <h4>requireJS 例子 example 01</h4> <div id="loadMsgCon" class="div01"></div> <script data-main="scripts/main" src="scripts/require.js"></script> <!--注意 requireJS 文件对应即可,同时data-main属性值写对即可--> </body> </html>
1,main.js
//1,about require js config//配置信息 ; require.config({ //define all js file path base on this base path //actually this can setting same to data-main attribute in script tag //定义所有JS文件的基本路径,实际这可跟script标签的data-main有相同的根路径 baseUrl:"./scripts" //define each js frame path, not need to add .js suffix name //定义各个JS框架路径名,不用加后缀 .js ,paths:{ "jquery":["http://libs.baidu.com/jquery/2.0.3/jquery", "lib/jquery/jquery.min"] //把对应的 jquery 这里写对即可 ,"workjs01":"work/workjs01" ,"workjs02":"work/workjs02" ,"underscore":"" //路径未提供,可网上搜索然后加上即可 } //include NOT AMD specification js frame code //包含其它非AMD规范的JS框架 ,shim:{ "underscore":{ "exports":"_" } } }); //2,about load each js code basing on different dependency //按不同先后的依赖关系加载各个JS文件 require(["jquery","workjs01"],function($,w1){ require(['workjs02']); });
2,workjs01.js
define(['jquery'],function($){ //注意模块的写法 //1,define intenal variable area//变量定义区 var myModule = {}; //推荐方式 var moduleName = "work module 01"; var version = "1.0.0"; //2,define intenal funciton area//函数定义区 var loadTip = function(tipMsg, refConId){ var tipMsg = tipMsg || "module is loaded finish."; if(undefined === refConId || null === refConId || "" === refConId+""){ alert(tipMsg); }else{ $('#' + (refConId+"")).html(tipMsg); } }; //3,should be return/output a object[exports/API] if other module need //如有需要暴露(返回)本模块API(相关定义的变量和函数)给外部其它模块使用 myModule.moduleName = moduleName; myModule.version = version; myModule.loadTip = loadTip; return myModule; /* //this is same to four line code upper//跟上面四行一样 return { "moduleName":"work module 01" ,"version":"1.0.0" ,loadTip:loadTip }; */ });
3, workjs02.js
define(['workjs01'],function(w01){ //1,define intenal variable area//变量定义区 var moduleName = "work module 02"; var moduleVersion = "1.0.0"; //2,define intenal funciton area//函数定义区 var setHtml = function(refId,strHtml){ if(undefined === refConId || null === refConId || "" === refConId+""){ return; }else{ $('#' + (refId + "")).html(strHtml+""); } }; //3,auto run when js file is loaded finish //在JS加载完,可在本模块尾部[return之前]运行某函数,即完成自动运行 w01.loadTip("本页文件都加载完成,本页设计 workjs01.js 文件依赖jquery, workjs02.js 依赖 workjs01.js","loadMsgCon"); //4,should be return/output a object[exports/API] //如有需要暴露(返回)本模块API(相关定义的变量和函数)给外部其它模块使用 return { "moduleName":moduleName ,"version": moduleVersion } });
三:运行页面
*
*
备注:公众号清汤袭人能找到我,那是随笔的地方
备注:公众号清汤袭人能找到我,那是随笔的地方