1. Use the module as the egress
We can use variables that need to be exposed, using a module as an exit
Common modular specifications: CommonJS, AMD, CMD, and Modules for ES6
2. CommonJS(Understand)
CommonJS export
module.exports = {
flag: true,
test(a, b) {
return a + b
}
}
Copy the code
CommonJS import
let { test, flag } = require('moduleA');
Copy the code
The above code only works in node.js and is used in Webpack.
3. ES6 modularity
3.1 Basic Use of export
//info.js export let name=" orange "//info.js let name=" orange" export{name} // export function test(content){ console.log(content) }Copy the code
3.2 the export default
In some cases, a module contains a function that we don’t want to name and let the importer name it. At this point, export Default can be used
// info.js
export default function(){
console.log("default function")
}
import myFunc from "./info.js"
myFun()
Copy the code
Export default cannot exist in the same module
Unified All Import
import * as aaa from "./info.js"
console.log(aaa.flag)
Copy the code