【问题标题】:What is the difference between annotation and decorator?注释和装饰器有什么区别?
【发布时间】:2016-09-16 00:11:43
【问题描述】:

我很困惑何时使用术语注释以及何时使用装饰器?

 @Component({
      selector: 'tabs',
      template: `
      `
    })
    export class Tabs {

    }

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    装饰器对应于在类上调用的函数,而注释是使用反射元数据库在类上设置的“唯一”元数据。

    对于 TypeScript 和 ES7,@Something 是一个装饰器。在 Angular2 的上下文中,@Component、@Injectable、... 等装饰器使用 Reflect.defineMetadata 方法为装饰元素定义元数据。

    您可能会对这个问题感兴趣,以了解装饰器实际上是什么:

    【讨论】:

    【解决方案2】:

    Traceur 给我们annotations。 TypeScript 给了我们decorators。但 Angular 2 两者都支持。

    注解创建一个“注解”数组。 而装饰器是接收装饰对象并可以对其进行任何更改的函数。

    由于 Angular 使用 TypeScript 而不是 atScript,所以它使用了装饰器。基本上有四种装饰器 有哪些

    • 类装饰器,例如@Component 和 @NgModule
    • 类内部属性的属性装饰器,例如@Input 和 @Output
    • 类内部方法的方法装饰器,例如@HostListener
    • 类构造函数内参数的参数装饰器,例如@注入

    更深入的可以参考

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 2020-02-21
      • 2023-03-21
      • 2011-02-04
      • 2018-07-19
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多