问题描述
我对何时使用术语注释和何时使用装饰器感到困惑?
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:
这篇关于注解和装饰器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!