export 暴露的接口 import {} from "模块名称/路径" 1. 定义模块,通过export暴露接口 //module1.jsexport let a = 3 ;export function sayHello(){}//index.jsimport {a,sayHello} from ‘./module1‘ 2. 定义模块,通过expo
export 暴露的接口
import {} from "模块名称/路径"
1. 定义模块,通过export暴露接口
//module1.js export let a = 3; export function sayHello(){ } //index.js import {a,sayHello} from ‘./module1‘
2. 定义模块,通过export default 暴露接口
//module1.js let a = 3; function sayHello(){ } export default { a, sayHello } //index.js import module1 from ‘./module1‘ module1.sayHello
3. nodejs如何支持es6的模块化
1) es6->es5
4. 案例:
//module1.js let name= "module1"; // 单独暴露 export function sayName(){ console.log("my name is",this.name); } export function sayHello(){ console.log("hello world"); } //index.js import {sayName} from ‘./module1‘
5. 案例
//module1.js let name= "module1"; function sayAge(){}, function sayGender(){} // 默认集体暴露 export default { sayAge, sayGender } //index.js import m from ‘./module1‘ m.sayAge()