# import与export
2 min read
Table of Contents
在 ES6 与 CommonJS 的模块系统中,导入与导出是区别的。
ES6
ES6 导出
// 单独导出export const a = 1
// 批量导出const b = 2const c = 3export { b, c }
// 导出函数export function f() {}
// 导出时取别名function f() {}export { f as F }
// 默认导出,无需函数名export default function () {}
// 引入外部模块,重新导出export { foo as bar } from './module'ES 导入
import { a, b, c } from './module' // 批量导入import { f as F } from './module' // 导入是起别名import * as Foo from './module' // 导入模块中的所有成员,绑定 Foo 上import Foo from './module' // 导入默认CommonJS
CommonJS 导出
// 整体导出let foo = { a: 1, b: 2,}module.exports = foo
// 导出多个变量// exports === module.exportsexports.x = 1exports.y = 2CommonJS 导入
const foo = require('./module')