Commonjs模块规范
module
就是代表这个模块,exports
就是对外的接口,加载模块,就是加载module.exports
属性。 exports
其实也是module.exports
,相当于在模块上添加 const exports = module.exports
使用require
进行导入模块
module.exports = xxx
export.xxx = xxx
module.exports = xxx
不能使用export.xxx
ES6模块
export
语句用于从文件(模块)中,导出实时绑定的函数、对象或者是值。导出的方式
默认导出
export defult xxx // 导出变量
export defult function // 导出 函数
export defult Class // 导出 类
命名导出
export { xxx }
导入
导入的时候,如果是默认导入,
import { xxx } from ‘..‘
import {xx as xxx} from ‘..‘
注意: 默认导出的话,在导入的时候引入的变量名,可以不相同
//x.js
export defult let a = 3
// y.js
import { b } from ‘./x.js‘
console.log(b) // 3
module.exports 和 exports , export defult 和 export 之间的区别
原文:https://www.cnblogs.com/zqfs433/p/13388729.html