【问题标题】:ES7 decorator doesn't work on class with "displayName" setES7 装饰器不适用于设置了“displayName”的类
【发布时间】:2015-11-11 05:40:13
【问题描述】:

我正在将 React DnD 添加到我正在构建的应用程序中,并且已经意识到使用以下语法的 ES7 装饰器不起作用(据我所知):

@DragDropContext(HTML5Backend)
export class App extends React.Component {
    ...
}
App.displayName = 'My App'

但是,如果我删除 App.displayName 行,一切正常。为什么?

我应该如何设置 displayName 属性?

【问题讨论】:

  • displayName 必须是原型上的属性吗?

标签: javascript reactjs decorator ecmascript-2016


【解决方案1】:

由于您已经在使用 ES7 功能,您可以使用 ES7 class properties。这些都在第 0 阶段。

@DragDropContext(HTML5Backend)
export class App extends React.Component {
    static displayName = 'My App';
    //or just
    //displayName = 'My App';
}

如果您使用的是 babel,则需要使用以下功能启用功能

babel --stage 0

babel --optional es7.classProperties

【讨论】:

  • 我认为我的设置有问题,因为某些 => 功能有效而其他功能无效。这是一个很好的提示。谢谢
猜你喜欢
  • 2017-10-03
  • 2018-12-06
  • 1970-01-01
  • 2012-06-20
  • 2016-02-11
  • 2015-11-24
  • 1970-01-01
  • 2021-07-05
  • 2016-06-30
相关资源
最近更新 更多