sea.js简单使用教程

  1. 下载sea.js, 并引入

  2. 创建项目结构

    |-js
    |-libs
      |-sea.js
    |-modules
      |-module1.js
      |-module2.js
      |-module3.js
      |-module4.js
      |-main.js
    |-index.html
  3. 定义sea.js的模块代码

    • module1.js

      define(function (require, exports, module) {
      //内部变量数据
      var data = 'atguigu.com'
      //内部函数
      function show() {
        console.log('module1 show() ' + data)
      }

      //向外暴露
      exports.show = show
      })
    • module2.js

      define(function (require, exports, module) {
      module.exports = {
        msg: 'I Will Back'
      }
      })
    • module3.js

      define(function (require, exports, module) {
      const API_KEY = 'abc123'
      exports.API_KEY = API_KEY
      })
    • module4.js

      define(function (require, exports, module) {
      //引入依赖模块(同步)
      var module2 = require('./module2')

      function show() {
        console.log('module4 show() ' + module2.msg)
      }

      exports.show = show
      //引入依赖模块(异步)
      require.async('./module3', function (m3) {
        console.log('异步引入依赖模块3 ' + m3.API_KEY)
      })
      })
    • main.js : 主(入口)模块

      define(function (require) {
      var m1 = require('./module1')
      var m4 = require('./module4')
      m1.show()
      m4.show()
      })
  4. index.html:

    <!--
    使用seajs:
    1. 引入sea.js库
    2. 如何定义导出模块 :
      define()
      exports
      module.exports
    3. 如何依赖模块:
      require()
    4. 如何使用模块:
      seajs.use()
    -->
    <script type="text/javascript" src="js/libs/sea.js"></script>
    <script type="text/javascript">
    seajs.use('./js/modules/main')
    </script>

最新文章

  1. jquery $.each的用法
  2. SQL Server中公用表表达式 CTE 递归的生成帮助数据,以及递归的典型应用
  3. Java学习笔记11
  4. Spring整合HBase
  5. 在eclipse中进行Struts2项目的配置
  6. unity3d学习重点记录
  7. shell中的expr命令
  8. swift 笔记 (二十) —— 泛型
  9. ajax 页面请求后,jsp页面定位
  10. AngularJS自定义指令之可选参数replace
  11. 【Python】正则表达式re
  12. __x__(39)0909第五天__ 表格 table
  13. 并发编程心得--synchronized
  14. 转载——githup的提交
  15. P3128 [USACO15DEC]最大流Max Flow
  16. jQuery Ajax 上传文件夹及文件
  17. UIAlertView 点击按钮后控制其是否消失
  18. jvm linux 时区设置
  19. SQL Server与Oracle对表添加列的不同点
  20. 《Programming with Objective-C》第五章 Customizing Existing Classes

热门文章

  1. Python第四天 流程控制 if else条件判断 for循环 while循环
  2. sqlserver数据库触发器调用外部exe
  3. WMware workstation中几种网络连接的说明 【转】
  4. SQLServer之创建事务未提交读
  5. LeetCode算法题-Repeated Substring Pattern(Java实现)
  6. Elixir 简介
  7. 推荐一套Angular2的UI模板
  8. DRF 序列化器-Serializer (2)
  9. python开发【lambda篇】
  10. TableExistsException: hbase:namespace