模块导出简介:module.exports与export

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011344924/article/details/84316289

module.exports

module.exports是Node中遵循的CommonJS模块规范。每个文件就是一个模块。

module代表当前模块;exports代表对外的接口。

Node为每个模块提供了一个exports变量,指向module.exports,(就如同在每个文件开头声明并赋值 var exports = modules.exports),所以不能将exports指向另一个值,因为这样等于切断了exports与module.exports之间的引用关系。

Node中使用require导入模块

export

export是ES6中的模块规范。

exportexport default:

  • export: 是导出一个个单独的接口,可以有多个,且可以直接导出变量表达式;导入时需要加{}
  • export default: 是为模块指定一个默认输出,是导出一个整体接口, 只有一个;导入时不需要加{}

ES6中使用import … from导入模块

猜你喜欢

转载自blog.csdn.net/u011344924/article/details/84316289