这次给大家带来require.js详解,使用require.js详解的注意事项有哪些,下面就是实战案例,一起来看一下。
index.html
<!DOCTYPE html><html>
<head>
</head>
<body>
<span>body</span>
</body>
//将require引入,放在html页面最下方,或设置异步请求
//即: <script src="js/lib/require.js" defer async="true"></script>
<script src="js/require.js" data-main="js/main"></script></html>main.js
require.config({ //配置路径
paths: { "jquery": ["http://libs.baidu.com/jquery/2.0.3/jquery", "jquery.min.js"], "a": "a"
}
})require(["jquery", "a"], function($,a) {
$(function() { //调用即可,此处的a并无意义,仅为指示,与引入文件顺序有关,即若
//function($,a)中a换了一种称谓,这里调用时保持一致即可
a.fun1();
alert("load finished");
})
})a.js
function fun1(){
alert("it works");
} //切记,如果需要在调用该函数,一定要记得返回,个人理解为暴露接口return { fun1: fun1
};
})```相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是Require.js详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号