你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

es6 export 和export default的区别

2022/6/27 6:10:34

转:https://www.jianshu.com/p/c6fa63c746d2

区别

export

  • 每个文件中可使用多次export命令
  • import时需要知道所加载的变量名或函数名
  • import时需要使用{},或者整体加载方法
exportexport default
每个文件中可使用多次export命令每个文件中只能使用一次export default命令
import时需要知道所加载的变量名或函数名import时可指定任意名字

export用法

a-1.js

export const name = 'tom'
export function say() {
  console.log(name)
}

a-2.js

import {name, say} from './a-1.js'

// 打印name
console.log(name)
// 调用say
say()

export default 用法

b-1.js

let obj = {
  name: 'tom',
  say() {
  console.log(this.name)
}
}
export default obj

b-2.js

import person from './b-1.js'
// 打印name
console.log(person.name)
// 调用say
person.say()