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: