sea.js简单使用教程
2024-10-12 03:29:58
sea.js简单使用教程
下载sea.js, 并引入
官网: http://seajs.org/
github : https://github.com/seajs/seajs
将sea.js导入项目: js/libs/sea.js
创建项目结构
|-js
|-libs
|-sea.js
|-modules
|-module1.js
|-module2.js
|-module3.js
|-module4.js
|-main.js
|-index.html定义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()
})
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>
最新文章
- jquery $.each的用法
- SQL Server中公用表表达式 CTE 递归的生成帮助数据,以及递归的典型应用
- Java学习笔记11
- Spring整合HBase
- 在eclipse中进行Struts2项目的配置
- unity3d学习重点记录
- shell中的expr命令
- swift 笔记 (二十) —— 泛型
- ajax 页面请求后,jsp页面定位
- AngularJS自定义指令之可选参数replace
- 【Python】正则表达式re
- __x__(39)0909第五天__ 表格 table
- 并发编程心得--synchronized
- 转载——githup的提交
- P3128 [USACO15DEC]最大流Max Flow
- jQuery Ajax 上传文件夹及文件
- UIAlertView 点击按钮后控制其是否消失
- jvm linux 时区设置
- SQL Server与Oracle对表添加列的不同点
- 《Programming with Objective-C》第五章 Customizing Existing Classes