Arrow使用Babel作为类属性

isa*_*sar 8 javascript class reactjs babeljs ecmascript-next

有人可以解释React中的Babel如何支持胖箭头函数作为类属性?使用Babel 尝试一下我可以看到它们不受支持:

class Question {

  // Property (not supported)
  myProp = () => {
    return 'Hello, world!';
  }

  // Method (supported)
  myFunc() {
    return 'Hello, world!';
  }

}
Run Code Online (Sandbox Code Playgroud)

ES6中不支持类属性(如果我错了,请纠正我),但是在React(使用Babel)中它们可以工作.

我可以使用TypeScript Playground看到方法和属性之间的区别,但我无法清楚地了解Babel是否支持它们.有插件吗?

更新:
我可以看到他们支持使用"babel-preset-stage-0".

GG.*_*GG. 4

要支持类属性,您需要安装并添加babel-plugin-transform-class-propertiesplugins您的设置.babelrc(或在您的webpack配置中)。

请注意,该插件也包含在

因此,如果您使用其中之一,则无需babel-plugin-transform-class-properties自行安装。