其中,index.html如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index</title> </head> <body> <script type="text/javascript" data-main="main.js" src="require.js"></script> </body> </html>
data-main為入口。
main.js如下:
define(['require','main'],function (require) { require(['index','test'],function (index,test) { console.log(index.index()); console.log(test.test()); }); });
index.js如下:
define(['require','index'],function (require) { var index = { index:function () { console.log("index"); } }; return index; });
test.js如下:
define(['require','test'],function (require) { var test = { test:function () { console.log("test"); } }; return test; });
當(dāng)要簡單包裝CommonJS來定義模塊,則main.js改為:
define(function(require, exports, module) { var index = require('index'); var test = require('test'); } );
即可引入index.js 和 test.js。
希望本文所述對(duì)大家RequireJS程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com