requirejs 介绍
Requirejs 是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一 。它非常适合在浏览器中使用,但它也可以用在其他脚本环境,就像 Rhino and Node。使用Requirejs加载模块化脚本将提高代码的加载速度和质量。
Requirejs优势:
1、实现js文件的异步加载,避免网页失去响应;
3、基于AMD模块化机制,让前端代码也能实现模块化。
Requirejs使用
例如,你的项目中有一个 project.html 页面和一些 scripts,目录布局如下:
项目目录/
project.html
scripts/
main.js
require.js
helper/
util.js
为了充分利用的优化工具,建议您将所有的scripts放到的html外面,然后只引用 require.js 来请求加载你其它的scripts:
<!DOCTYPE html>
<html>
<head>
<title>My Sample Project</title>
<!-- data-main attribute tells require.js to load
scripts/main.js after require.js loads. -->
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
<body>
<h1>My Sample Project</h1>
</body>
</html>
在 main.js,你可以使用 require() 来加载所有你需要运行的scripts. 这可以确保你所有的scripts都是在这里加载的
require(["helper/util"],function(util) {
//This function is called when scripts/helper/util.js is loaded.
//If util.js calls define(),then this function is not fired until
//util's dependencies have loaded,and the util argument will hold
//the module value for "helper/util".
});
GitHub:https://github.com/requirejs/requirejs
requirejs
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。