注解和装饰器有什么区别? [英] What is the difference between annotation and decorator?

查看:432
本文介绍了注解和装饰器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对何时使用术语注释和何时使用装饰器感到困惑?

I am confused when to use term annotation and when to use decorator ?

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

    }

推荐答案

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

A decorator corresponds to a function that is called on the class whereas annotations are "only" metadata set on the class using the Reflect Metadata library.

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

With TypeScript and ES7, @Something is a decorator. In the context of Angular2, decorators like @Component, @Injectable, ... define metadata for the decorated element using the Reflect.defineMetadata method.

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

This question could interest you to find out what a decorator actually is:

这篇关于注解和装饰器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆