require JS入门 及 打包
2016-03-25 13:43
513 查看
require Js 入门
代码库结构示例:
其中部分文件代码及作用解析如下:
index.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script data-main="js/main.js" src="js/require.js"></script> </head> <body> <p>hi~</p> </body> </html>
js/main.js:
js入口文件,其中设置基本参数,调用函数。
require.config({ baseUrl: 'js', /*相对于根路径的绝对路径,与main.js本身的位置没有关系*/ paths: { jquery: 'jquery/jquery-1.11.1', /*不用加后缀.js*/ aaa: 'jquery/jquery-aaa', values: 'other_js/values', tourl: 'other_js/tourl' } }); // Start the main logic. require(['jquery', 'aaa', 'values', 'tourl'], function ($, a, v, t) { $(document).ready(function () { t;/*注释后一样执行,因为引用的时候相当于调用了。*/ a.al();/*若想手动调用其中函数,可以设置返回值,然后手动调用*/ }); });
js/other_js/tourl.js:
define(["require"], function(require) { return alert(require.toUrl("./style.css")); });
js/jquery/jquery.aaa.js:
define (function () { return { al: function() {alert('kk');} }; });
执行index.html代码后,结果如图:
第一次弹框:
第二次弹框:
require Js 利用r.js打包 (以下经验借鉴自他人博客点击打开原文链接)
首先下载r,js,然后保证需要打包以及涉及到的文件处于同一目录下。代码库结构示例:
其中部分文件代码如下:
index.html
相关文章推荐
- iOS 下拉刷新 UIRefreshControl
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- UIView的生命周期总结
- UIView的生命周期
- 自动生成button点击获取value jqyery
- Android 更新UI的两种方法——handler和runOnUiThread()
- UIViewController的 modalPresentationStyle 用法
- ArcGIS Runtime+Simple MVVM VM操纵UI对象
- UIAlertController
- mybatis报错(一)报错Result Maps collection does not contain value for java.lang.Integer解决方法
- MySQL之——Ignoring query to other database
- ngui学习之scrollview
- 去除UISearchBar中的clearButton(输入内容时出现的删除按钮)
- numpy.ones_like(a, dtype=None, order='K', subok=True)返回和原矩阵一样形状的1矩阵
- iOS (UIButton封装)仿糯米首页缩放“按钮”效果
- NGUI裁剪模型和粒子
- GLSurfaceView queueEvent的作用
- UIView不接收触摸事件的三种情况
- checkio the most frequent weekdays
- CActiveXUI的一个Bug