require.js初识

一、作用

1、实现js文件的异步加载,避免网页失去响应。
2、管理模块之间的依赖性,便于代码的编写和维护。

二、使用

嵌入require到页面

 

<script data-main="scripts/main" src="scripts/require.js"></script>
其中:main.js用来配置相关模块,举个栗子:scripts/main

 require.config({
      paths : {
        "jquery" : "scripts/jquery.min",
        "a" : "scripts/a",
        "bootstrap":"scripts/bootstrap.min"
        },
        shim:{
        	'bootstrap':{'deps':'jquery'}//表示bootstrap对jquery有依赖
        }
     })



 

 

 

 

 

 

在index.html中引入:<script data-main="scripts/main" src="scripts/require.js"></script>后,使用define(//前后参数需要一一对应)

 

    define(['jquery','bootstrap','a'],function($,Bootstrap){
            //doSomething
          })

 

 

 


附:官方文档:http://requirejs.org/

       一看就懂的阮一峰的博客:http://javascript.ruanyifeng.com/tool/requirejs.html

 

后续会学习sea.js。并进行比较,以及AMD和CMD,对应于不同的应用场景。

 

http://www.haosf80.com/

 

没什么大不了,只要每天太阳依旧升起,一切都是美好的~~~
原文地址:https://www.cnblogs.com/xujjxin/p/4501695.html