有人可以解释一下React中的Babel如何支持粗箭头功能作为类属性吗?使用Babel 尝试一下,我可以看到它们不受支持:
class Question { // Property (not supported) myProp = () => { return 'Hello, world!'; } // Method (supported) myFunc() { return 'Hello, world!'; } }
ES6不支持类属性(如果我错了,请纠正我),但是在React(使用Babel)中它们可以工作。
我可以看到使用TypeScript Playground的方法和属性之间的区别,但我不清楚Babel是否支持它们。有插件吗?
更新: 我可以看到他们支持使用"babel-preset-stage-0"。
"babel-preset-stage-0"
为了支持类属性,您需要安装并添加babel-plugin-transform-class- properties到plugins您的设置中.babelrc(或在您的webpack配置中)。
babel-plugin-transform-class- properties
plugins
.babelrc
webpack
请注意,此插件也包含在
babel-preset-stage-0
babel-preset-stage-1
babel-preset-stage-2
因此,如果您使用其中之一,则无需babel-plugin-transform-class-properties自己安装。
babel-plugin-transform-class-properties