RequireJS用法简单示例
本文实例讲述了RequireJS用法。分享给大家供大家参考,具体如下:
建立如下工程目录:
|-test
|-index.html
|-index.js
|-main.js
|-require.min.js
|-test.js
其中,index.html如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index</title> </head> <body> <script type="text/javascript" data-main="main.js" src="require.js"></script> </body> </html>
data-main为入口。
main.js如下:
define(['require','main'],function (require) { require(['index','test'],function (index,test) { console.log(index.index()); console.log(test.test()); }); });
index.js如下:
define(['require','index'],function (require) { var index = { index:function () { console.log("index"); } }; return index; });
test.js如下:
define(['require','test'],function (require) { var test = { test:function () { console.log("test"); } }; return test; });
当要简单包装CommonJS来定义模块,则main.js改为:
define(function(require, exports, module) { var index = require('index'); var test = require('test'); } );
即可引入index.js 和 test.js。
希望本文所述对大家RequireJS程序设计有所帮助。
相关文章
js/jquery遍历对象和数组的方法分析【forEach,map与each方法】
这篇文章主要介绍了js/jquery遍历对象和数组的方法,结合实例形式分析了数组遍历的forEach,map与each方法常见使用技巧,需要的朋友可以参考下2019-02-02Bootstrap基本插件学习笔记之Alert警告框(20)
这篇文章主要为大家详细介绍了Bootstrap基本插件学习笔记之ALert警告框的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-12-12学习javascript面向对象 理解javascript对象
这篇文章主要介绍了javascript对象,学习javascript面向对象,感兴趣的小伙伴们可以参考一下2016-01-01
最新评论