裝飾器主要用于將一些常用操作進(jìn)行抽象出一層對類,方法,屬性,參數(shù)進(jìn)行修飾的函數(shù)。
常用例子:設(shè)置屬性時(shí),打日志等。
此篇主要分享一篇覺得易懂的博文,見底部引用
裝飾器的分類
類裝飾器 (Class decorators)
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void
屬性裝飾器 (Property decorators)
declare type PropertyDecorator = (target:Object, propertyKey: string | symbol ) => void;
方法裝飾器 (Method decorators)
declare type MethodDecorator = <T>(target:Object, propertyKey: string | symbol, descriptor: TypePropertyDescript<T>) => TypedPropertyDescriptor<T> | void;
參數(shù)裝飾器 (Parameter decorators)
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number ) => void