requirejs簡單應用

來源:互聯網
上載者:User

標籤:download   檔案中   return   css   detail   csdn   conf   編寫   自訂   

1、首先匯入相應的js檔案

    <script src="js/require.js"></script>    <script src="js/mian.js"></script>    <script src="js/index.js"></script>
require.js是requirejs的庫,mian.js是用來寫require配置資訊的,而index是寫自己的邏輯代碼
2、編寫mian.js配置資訊
require.config({    baseUrl:‘js/‘,//定義跟路徑    paths:{        ‘jquery‘:‘jquery‘,        ‘bootstrap‘:‘bootstrap.min‘,        ‘utils‘:‘utils‘    },    //依賴關係,載入boostrap之前載入jQuery    shim:{        ‘bootstrap‘:{            ‘deps‘:[‘jquery‘]        }    }});

3、然後在自己的index.js檔案中實現自己的代碼

//單純的引入jQuery庫    require([ ‘jquery‘], function($) {        $("body").css({            "background-color" : "red"        });    });

通過依賴關係引入js庫

//在mian.js中依賴關係,載入boostrap之前載入jQuery    shim:{        ‘bootstrap‘:{            ‘deps‘:[‘jquery‘]        }    }//然後在自己的index.js//通過依賴在引入boostrap庫時先引入jQuery庫    require([ ‘bootstrap‘], function() {        $("body").css({            "background-color" : "red"        });    });

4、定義自己的utils.js庫

define ([‘jquery‘],function($){    return {        change:function(){            $("body").css({                "background-color" : "red"            });        },        show:function(){            alert("show");        }        }});

//在自己的js檔案中


//自訂自己的庫,然後引入
require([‘utils‘], function(utils) {
utils.show();
utils.change();
});

資源:http://download.csdn.net/detail/u013865056/9915553

 

requirejs簡單應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.