requirejs

编程之家收集整理的这个编程导航主要介绍了requirejs编程之家,现在分享给大家,也给大家做个参考。

requirejs

requirejs 介绍

Requirejs 是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一  。它非常适合在浏览器中使用,但它也可以用在其他脚本环境,就像 Rhino and Node。使用Requirejs加载模块化脚本将提高代码的加载速度和质量。  

Requirejs优势:

1、实现js文件的异步加载,避免网页失去响应;

2、管理模块之间的依赖性,便于代码的编写和维护。

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".

});

网站地址:http://requirejs.org/

GitHub:https://github.com/requirejs/requirejs

网站描述:JavaScript的一个文件和模块加载器

requirejs官方网站

官方网站:http://requirejs.org/

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

相关文章

jade,Jade 是一个高性能的模板引擎,以 JavaScript 的形式集...
curl.js,轻量级,快速,可扩展的模块加载
jspm-cli,基于SystemJS的Js包管理器
angular-cli,快速构建Angular2项目
lerna,一款多项目多模块的管理工具
duo.js,前端的下一代包管理器。