SeaJS
3246 ワード
インストールと使用
手順: seajsファイル を導入メインモジュールファイル を定義する.メインモジュールファイルdefine定義モジュールを介して、共通メンバー をエクスポートする.ページスクリプト コールバック関数のパラメータが渡されたのは、モジュールから導出されたメンバー である.
定義モジュールは、以下のようにモジュールを定義します.
モジュールの使用 seajs.useは一般的に入口モジュールに用いられ、一般的には1回の しか使用されない. requireモジュールとモジュール間
メンバーのエクスポート方法 module.exports exports.xxx return
非同期呼び出しは次の呼び出しでrequireが実行されてから実行されます.
requireを使用する.async非同期呼び出し、まず03-module 2をロードする.jsファイル、ロード後functionコールバック関数を実行
**サードパーティ依存(jQuery)**jqueryはAMD仕様をデフォルトでサポートしています.define関数を使用すると、CMDが国産であるため、jqueryはデフォルトでサポートされていません.以下の改造により
Seajsの構成の参照はseajs.configでは、calc.jsパスが変化すると、別名の内容を変更するだけで、コードのパスはseajsを通過するように構成されています.use('calc')別名の使用
npm install seajs
でインストール手順:
seajs.use('path',fn)
使用モジュール定義モジュールは、以下のようにモジュールを定義します.
define(function(require, exports, module) {
//
//
// 01-convertor
var convertor = require('./01-convertor.js');
function add(a, b) {
return convertor.convertToNumber(a) + convertor.convertToNumber(b);
}
//
exports.add = add;
});
モジュールの使用
// Seajs
// Seajs
seajs.use('./01-calculator.js', function(calculator) {
var ta = document.getElementById('txt_a');
btn.onclick = function() {
switch (op.value) {
case '+':
tres.value = calculator.add(ta.value, tb.value);
break;
}
};
});
メンバーのエクスポート方法
console.log(module.exports === exports);
出力はtrueです. // exports module.exports ,
exports.name = 'hello';
// module.exports , module.exports
module.exports = { name: 'hello' };
// return
return { name: 'world' };
非同期呼び出しは次の呼び出しでrequireが実行されてから実行されます.
console.log('module1 ---- start');
// require (./module2.js )
var module2 = require('./03-module2.js'); //
// JS
console.log('module1 ---- end');
requireを使用する.async非同期呼び出し、まず03-module 2をロードする.jsファイル、ロード後functionコールバック関数を実行
console.log('module1 ---- start');
require.async('./03-module2.js', function(module2) {
}); //
console.log('module1 ---- end');
**サードパーティ依存(jQuery)**jqueryはAMD仕様をデフォルトでサポートしています.define関数を使用すると、CMDが国産であるため、jqueryはデフォルトでサポートされていません.以下の改造により
// CMD
if (typeof define === "function" && !define.amd) {
// define , AMD
// jquery AMD CMD , jquery
define(function() {
return jQuery.noConflict(true);
});
}
Seajsの構成の参照はseajs.configでは、calc.jsパスが変化すると、別名の内容を変更するだけで、コードのパスはseajsを通過するように構成されています.use('calc')別名の使用
seajs.config({
alias: {
calc: './modules/calc.js',
}
});
seajs.use('calc');
</code></pre>
</article>
</div>
</div>
</div>
<!--PC WAP -->
<div id="SOHUCS" sid="1181744736059359232"></div>
<script type="text/javascript" src="/views/front/js/chanyan.js">