1.首先说 exports 和 module.exports
1).exports是module.exports的一个引用,这两个是commonjs的规范,require是AMD规范引入格式;
2).如果要对外暴露属性或方法,就用 exports ,要暴露对象(类似class,包含了很多属性和方法),就用 module.exports;
3).导出和引入 , exports 在此不再赘述,用的比较少,大家可自行搜索
modules.exports 导出和引入
导出
function a(){}
let b = '123'
module.exports = { a , b }
引入
var xxx = require('./1.js')
xxx.a() xxx.b
package.json中type需要设置为commonjs, package.json文件非必须
"type":"commonjs"
2. export 和 export default
1).这两个是es6语法,需要在package.json中设置 type:module
export 导出和引入
导出
function xxx(){}
export { xxx }
引入
import { xxx } from './1.js'
xxx()
或者
import * as Test from './1.js'
Test.xxx()
可以有多个export, 引入需要解构
export default 一个文件模块中只能存在一个
导出
function xxx(){}
let a = '123'
export default {xxx,a}
引入
import name from './1.js'
name.xxx()
console.log(name.a)
如有错误,还望指正