typescript类装饰器
//类装饰器(无法传参) function logClass(params:any){//params就是当前的类 console.log(params) params.prototype.apiUrl = '动态扩展的属性'; params.prototype.run = function(){ console.log('我是一个run方法'); } } @logClass//后面不可以加分号 class HttpClient{ constructor(){ } getData():void{ console.log(11) } } let http:any = new HttpClient(); console.log(http.apiUrl); http.run(); //类装饰器(可以传参,装饰器工厂) function decoClass(params:string){ return function(target:any){ console.log(target);//类Client console.log(params);//传进来的参 target.prototype.apiUrl = params; } } @decoClass('hello')//后面不可以加分号 class Client{ constructor(){ } getData(){ } } let client:any = new HttpClient(); console.log(client.apiUrl)