【问题标题】:Unexpected token on "export default const"“导出默认常量”上的意外令牌
【发布时间】:2017-03-11 19:27:07
【问题描述】:

我有两个不同的本地项目,它们的库版本完全相同。

但是最新的一个在“export default const”上失败了,另一个没有。

这两个调用有什么区别?

第一个编译正确并且已经在应用商店中,代码如下:

export default const result = [...]

第二个具有相同的 package.json 并在相同的代码上失败“意外令牌 (1:15) -> 位置 15 在“默认”之后。

这是使用的package.json:

{
  "name": "rn_simpleorm",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "react-native start"
  },
  "dependencies": {
    "react": "15.3.2",
    "react-native": "^0.32.0"
  },
  "jest": {
    "preset": "jest-react-native",
    "modulePathIgnorePatterns": [
      "node_modules/react-native/node_modules/"
    ]
  },
  "devDependencies": {
    "babel-jest": "^15.0.0",
    "babel-preset-react-native": "^1.9.0",
    "jest": "^15.1.1",
    "jest-react-native": "^15.0.0",
    "react-test-renderer": "^15.3.1"
  }

}

【问题讨论】:

    标签: javascript react-native ecmascript-6


    【解决方案1】:

    您正在导出一个值。 const result = 不是一个值。你想要的是:

    export default [...];
    

    或:

    const result = [...];
    export default result;
    

    【讨论】:

    • 那为什么export default class TestClass extends React.Component {} 有效?
    • 这是一个类声明,工作方式不同。同样,export default function foo() 也可以。如果您尝试过export default const cls = class TestClass,它也不会起作用。
    • 好吧,我只是希望export default 在继续之前等待右侧编译,这就是为什么我没想到以这种方式导出类是一个问题
    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2017-03-05
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多