【问题标题】:why es6 expport and import is messy为什么 es6 导出和导入很乱
【发布时间】:2017-12-11 04:55:45
【问题描述】:

我正在使用 babel es6 类:

export class Util{
  async stringy(str){
    return await str
  }
}

然后我导入它

import Util from '../lib/util'

但它是未定义的。

【问题讨论】:

  • 您需要使用export default或使用import { Util }
  • 一点也不乱(虽然这有点主观)——那是因为你用错了……

标签: javascript ecmascript-6 babeljs


【解决方案1】:

正如 4castle 所说,您混淆了导出/导入类型。它应该是:

// util.js
export class Util{
  async stringy(str){
    return await str
  }
}

// other.js
import { Util } from '../lib/util'

// util.js
export default class Util{
  async stringy(str){
    return await str
  }
}

// other.js
import Util from '../lib/util'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2017-02-23
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2017-08-13
    • 2017-11-23
    相关资源
    最近更新 更多