moudle.js使用

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   io   

module.js類似於seaJs這些模組載入,不過很輕便。這裡就是簡單的介紹一下如何使用它。這裡是git地址https://github.com/eccued/modulejs

假如在頁面中使用module.js,首先我們先配置依賴,把需要的包引進來。

//包依賴配置
_moduleConfig = {
alias: {
‘zepto‘:‘libs/zepto.js‘,//需要用到的架構
‘cookie‘:‘js/cookie.js‘,//需要用到的基礎方法函數
‘action‘:‘js/init.js‘//整個頁面的啟動js
}
}

然後在這個頁面中引入改module的js。

    <script type="text/javascript" src="libs/module.js" ></script>    <script>    modulejs([‘action‘], function() { //‘action‘就是上面配置的依賴的包        var A = require(‘action‘);        A.init();    });        </script>

然後再來看看init.js。

define("action", function (require, exports, module) {    var $ = require(‘zepto‘);//引入配置的包    var _cookie = require(‘cookie‘);//引入配置的包    var obj = {},that = obj;    obj.getData = function(){        //dosomething        }    obj.addEvent = function(){        //dosomething    }    // 對外暴露介面    exports.init = function(){        that.getData();//取資料        that.addEvent();//綁定事件    };});

大概就是這樣就實現了js代碼分離了,這樣一來就可以讓你的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.