TS 装饰器

175 阅读1分钟

定义

装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,访问符,属性或参数上。 装饰器使用@expression形式,expression求值后必须为一个函数,会在运行时被调用,被装饰的声明信息作为参数传入 使用装饰器,必须在tsconfig.json中设置experimentalDecorators为true

"compilerOptions":{
   "experimentalDecorators":true
}

多个装饰器的执行顺序

收集的时候是从上到下依次收集,执行时时从下到上执行

function test1(constructor:any){
   console.log('decorator1')
}
function test2(constructor:amy){
   console.log('decorator2')
}
@test1
@test2
// 执行顺序
// decorator2
// decorator1