ES6的模块化
ES6的模块化
模块的思想,将对应的功能代码封装为一个模块(js代码 css代码 html代码)。
想要使用别人就导入,想要给别人用就导出。复用。
amd (在对应的加载之前导入)
cmd (在用的时候导入)
comment.js (基于amd和cmd之上)
es6的模块化的关键词(要想导入想要导出)
import 导入
export 导出
export的使用
第一种 export default (只能声明一次)
// //默认导出只有一个 如果使用export default 导出的可以使用对应的一个名字来接
export default {
obj,
str,
say
}
接收
import obj from './test.js'
console.log(obj) //{obj,str,say}
第二种导出
//如果直接使用的对应的export 导出那么必须通过{键}来接
export const obj = {
name:'jack',
age:18
}
//如果导出的是值类型 一定要写变量名
export const str = "你好世界"
export const say = ()=>{
console.log('hello world');
}
接收
import {obj,str,say} from './test.js'
第三种导出
const name = 'tom'
//第三种导出
export {
//只能写变量
name
}
接收
import {name} from './test.js'
import 名字 from '地址'
浙公网安备 33010602011771号