码迷,mamicode.com
首页 > 其他好文 > 详细

有关es6的模块化

时间:2020-01-23 18:29:11      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:UNC   conf   module   code   port   es6   func   LLC   指定   

假如有两个文件:app.js和config.js

app.js为主文件要去引用config这个模块

以前学习node时使用的模块导出:

需求:导出三个成员,分别是 foo: bar f: function num: 10

exports.foo = 'bar'
exports.f = function () {
  
}
exports.num = 10

es6中的模块导出

方法一

export const foo = 'bar'
export const f = function () {
  
}
export const num = 10

两种可以混合使用
方法二

const foo = 'bar'
const f = function () {
  
}
const num = 10

export {
  foo,
  f,
  num
}

通过 export 导出的成员必须通过解构赋值按需加载
或者通过import * as 变量名的形式加载所有通过 export 关键字导出的接口成员
通过 export default 加载导出的成员必须通过 import 变量名 from ‘模块标识‘ 进行加载
export 和 export default 可以共存

import config from './config'
import { foo, num } from './config'
import * as config from './config'
import { foo } from './config'

默认导出

config.js

// 相当于 module.exports = function () {}
export default function () {
  console.log('fff')
}

app.js:

// 这种方式会去找被加载模块中通过 export default 导出的成员
import defaultConfig from './config'

defaultConfig任意指定

最后通过模板字符串将它们打印出来

console.log(default: ${defaultConfig}, foo: ${foo}, all: ${allConfig})

有关es6的模块化

标签:UNC   conf   module   code   port   es6   func   LLC   指定   

原文地址:https://www.cnblogs.com/ygjzs/p/12230863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!