Seajs的用法

Seajs官方中文文档:http://seajs.org/docs/#quick-start

新建一个HTML文件index.html,在index.html文件页尾,通过<script>引入sea.js后,有一段配置代码:

// seajs 的简单配置
seajs.config({
  base: "../sea-modules/",
  alias: {
    "jquery": "jquery/jquery/1.10.1/jquery.js"
  }
})

// 加载入口模块
seajs.use("../static/hello/src/main")

sea.js在下载完成后,会自动加载入口模块,页面中的代码就是这么简单。

模块代码:

这个index.html文件有两个模块spinning.js和main.js,遵循统一的写法:

// 所有模块都通过 define 来定义
define(function(require, exports, module) {

  // 通过 require 引入依赖
  var $ = require('jquery');
  var Spinning = require('./spinning');

  // 通过 exports 对外提供接口
  exports.doSomething = ...

  // 或者通过 module.exports 提供整个接口
  module.exports = ...

});

上面就是 Sea.js 推荐的 CMD 模块书写格式。如果你有使用过 Node.js,一切都很自然。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章