问题描述
有人可以解释一下React中的Babel如何支持胖箭头功能作为类属性吗?使用巴别我可以看到它们不受支持:
Can someone explain how Babel in React supports fat arrow functions as class properties?Using Babel Try it out I can see they are not supported:
class Question {
// Property (not supported)
myProp = () => {
return 'Hello, world!';
}
// Method (supported)
myFunc() {
return 'Hello, world!';
}
}
ES6不支持类属性(如果我错了,请纠正我),但是在React(使用Babel)中它们可以工作.
Class properties are not supported in ES6 (correct me if I'm wrong) but then in React (with Babel) they work.
我可以看到使用TypeScript 游乐场但我不清楚了解Babel是否支持他们.有插件吗?
I can see the difference between methods and properties using TypeScript Playground but I can't clearly understand if Babel is supporting them or not.Is there some plug-in?
更新:
我可以使用 "babel-preset-stage-0"
看到它们的支持.
UPDATE:
I can see they are supported using "babel-preset-stage-0"
.
推荐答案
要支持类属性,您需要安装并添加 babel-plugin-transform-class-properties
设置为.babelrc
(或在webpack
配置中)的plugins
设置.
To support class properties, you need to install and add babel-plugin-transform-class-properties
to the plugins
setting of your .babelrc
(or in your webpack
config).
请注意,此插件也包含在
Note that this plugin is also included in
因此,如果您使用其中之一,则无需自己安装babel-plugin-transform-class-properties
.
So if you use one of those, you don't need to install babel-plugin-transform-class-properties
by yourself.
这篇关于使用Babel将Arrow用作类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!