【轉】require.js學習筆記(二)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   java   strong   sp   

require.js遵循AMD規範,通過define定義模組,require非同步載入模組,一個js檔案即一個模組。

一、模組載入require
1.載入符合AMD規範模組

HTML:

<script src="js/require.js" data-main="js/main"></script>

 

MAIN.JS

  require.config({

    baseUrl: "js/lib",

    paths: {

      "jquery": "jquery.min"

    }

  });


require([‘jquery‘], function ($){    // some code here  });

 

2.載入不符合AMD規範模組

require.config({    shim: {      ‘underscore‘:{       exports: ‘_‘        },      ‘backbone‘: {        deps: [‘underscore‘, ‘jquery‘],        exports: ‘Backbone‘      }    }  });

 

 

 

二、模組定義define

define([‘math‘, ‘graph‘],     function ( math, graph ) {        return {            plot: function(x, y){                return graph.drawPie(math.randomGrid(x,y));            }        }    };);

 

 

三、require+jquery+domready

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <script src="js/require.js"></script></head><body>    <div id="333">123124</div>    <script>     require.config({        paths: {"jquery": "js/lib/jquery-1.11.1.min",        "domReady": "js/lib/domReady"    }      });    require(["domReady!", "jquery"], function() {              //alert(‘22‘)             change();     });     function change(){        $(‘#333‘).text(‘5555‘);    }     </script>         </body></html>

 

 

 


原文地址:
http://javascript.ruanyifeng.com/tool/requirejs.html

http://www.ruanyifeng.com/blog/2012/11/require_js.html



【轉】require.js學習筆記(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.