1、常规导出
(1)分开导入导出
export xxx;
import {xxx} from "path";
(2)一次性导入导出
export {xxx, yyy, zzz};
import {xxx, yyy, zzz} from "path";
如果想修改接收变量名,可以通过 xxx as newName方式,变量名被修改后,原有变量名自动失效。
a.js
let name = "lnj";
export {name};
let age = 33;
export {age};
index.js
import {name} from "./a.js";
import {name, age} from "./a.js";
修改变量名:
import {name as str} from "./a.js";
2、默认导入导出
export default xxx;
import xxx from "path";
注意点:
一个模块只能使用一次默认导出, 多次无效。
默认导出时, 导入的名称可以和导出的名称不一致。
let name = "lnj";
let age = 33;
function say() {
console.log("hi");
}
export {name, age, say};
class Person {
constructor(){
this.name = "zs";
this.age = 18;
}
}
export default Person;
index.js
// 两种方式混合使用
import Person,{name, age, say} from "./c.js";