修饰器只可以修饰类和类里面的方法
修饰器方法没有带参数时
const logTime=(target: any, key: string, descriptor: PropertyDescriptor)=> {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.time(key);
const result = originalMethod.apply(this, args);
console.timeEnd(key);
return result;
};
return descriptor;
}
class Person {
constructor(public name: string) {}
@logTime
sayHello() {
console.log(`Hello, my name is ${this.name}.`);
}
@logTime
sayGoodbye() {
console.log(`Goodbye, ${this.name}.`);
}
}
const john = new Person('John');
john.sayHello(); // 输出 Hello, my name is John. 和方法的执行时间
john.sayGoodbye(); // 输出 Goodbye, John. 和方法的执行时间
打印的结果
修饰器方法带参数
import { PutForm, PutJson, PostJson, PostForm, Delete, Get ,asdasd} from '@shared/utils/http-util';
@asdasd('base/store/regedit','asd','asdasd')
regedit(params: any): Observable<any> {
return;
}
打印结果