export exposed interface
import {} from “module name/path”
1. Define the module and expose the interface through export
//module1.js
export let a = 3;
export function sayHello(){
}
//index.js
import {a,sayHello} from './module1'
2. Define the module and expose the interface through export default
//module1.js
let a = 3;
function sayHello(){
}
export default {
a,
sayHello
}
//index.js
import module1 from ‘./module1‘
module1.sayHello
3. How does nodejs support es6 modules Transformation
1) es6->es5
4. Case:
//module1.js
let name= "module1";
// separate exposure
export function sayName(){
console.log("my name is",this.name) ;
}
export function sayHello(){
console.log("hello world");
}
//index.js
import {sayName} from'./module1'
5. Cases
< div class="code">
//module1.js
let name= "module1";
function sayAge(){},
function sayGender(){}
// Default collective exposure
export default {
sayAge,
sayGender
}
//index.js
import m from ‘./module1‘
m.sayAge()