export与import
es6中export和import的用法:
1.
export
a.js
//方式一:
export var name = "wade";
export var age = 20;
//方式二: var name = "wade"; var age = 20; export {name, age }
//方式三(用as关键字重命名): var name = "wade"; var age = 20; export { name as n, age as a }
import
b.js
//方式一、方式二 import { name, age } from "./a.js"
//方式三: import { n, a } from "./a.js"
使用import命令的时候,需要知道加载的变量名或函数名,否则无法加载。
2.模块的整体加载:
a.js var name = "wade"; var age = 20; export { name, age } b.js import * as person from "./a.js" var n = person.name; var a = person.age;
3.export default默认导出:
a.js var person = { name: "wade", age: 20 } export default person b.js import p from "./a.js" var n = p.name; var a = p.age;
一个模块只能有一个默认输出,因此export default命令只能使用一次