基本用法
- es6 中新增了两个命令 export 和 import ,
- export 命令用于规定模块的对外接口,
- import 命令用于输入其他模块提供的功能。
import三种方式:
- 1、暴露模块 分别暴露
- 2、暴露模块 统一暴露
- 3、暴露模块 默认暴露 可以暴露 任意数据类型; 暴露的什么数据 接收到的就是什么数据
第一种方式:module1.js
// 暴露模块 分别暴露
export function foo(){
console.log('foo() module1')
}
export function bar(){
console.log('bar() module1')
}
export let arr = [1,2,3,4,5,6]
第二种方式:module2.js
//暴露模块 统一暴露
function fun(){
console.log('fun() module1')
}
function fun2 (){
console.log('fun() module2')
}
export{ fun, fun2}
第三种方式:module3.js
//暴露模块 默认暴露 可以暴露 任意数据类型; 暴露的什么数据 接收到的就是什么数据
//export default value
//1暴露的方法 一个脚本里面有一个 export default
export default ()=>{
console.log('默认暴露 写什么暴露什么')
}
//2暴露对象
export default{
msg:'我是谁我在哪',
foo(){
}
}
在 Mian.js 中的使用
//引入其他模块
import {foo,bar} from './module1'
import {fun,fun2} from './module2'
import module3 from './module3'
console.log(foo)
console.log('>>>>>>>>>>>')
console.log(fun)
//当为方法时
module3()
//当为对象时
module3.foo()