var name="tom"
var age="18"
export {name,age}
export default "jack"
1.导入模块的具名成员
import {name,age} from "./test"
console.log(name)//tom
2.导入模块的默认成员
import otherName from "./test"
console.log(otherName )//jack
3.导入模块的所有成员
import * as name from "./test"
console.log(name)//{name:tom,age:18}
4.同时导入模块的具名成员和默认成员
(1)import otherName ,{name,age} from "./test"
(2)import {name,age,default as otherName } from "./test"
5.动态导入模块
首先我们都知道 import语法只能出现在js的最上面去使用,在其他位置去导入模块是无效的。
如果我们需要动态的去导入模块的话,就需要使用import()语法糖,这个语法糖返回的是一个promise,具体用法如下:
......
function test (){
import ("./test.js").then(function(module){
console.log(module)
})
}
......