装饰器是一种特殊的类型声明,它能够被附加到类的声明、方法、访问符、属性或者参数上,可以修改类的行为。
装饰器使用@expression这种形式, expression求值后必须是一个函数,它会在运行时被调运,被装饰的声明信息作为参数传入。

1. 类装饰器

1.1. 类的普通装饰器(没有参数)

类装饰器: 类装饰器是在类声明之前被声明(紧靠着类声明)。装饰器应用于类构造函数, 可以用来监视,修改或替换类定义,传入一个参数 .
普通装饰器装饰器

  1. function logClass(params: any){
  2. console.log(params) //params就是当前的类
  3. //可以通过prototype给装饰器添加属性和方法
  4. params.prototype.apiUrl = 'www.baidu.com' //动态扩展的属性
  5. params.prototype.run = function(){
  6. console.log('我是装饰器动态扩展的方法')
  7. } }
  8. @logClass class HttpClient{
  9. constructor(){ }
  10. getData(){ }
  11. }
  12. var http:any = new HttpClient();
  13. console.log(http.apiUrl)
  14. http.run()

1.2. 类的装饰器工厂(可传参数)

  1. function logClass(params: string){
  2. return function(target: any){
  3. console.log(params);
  4. console.log(target); //www.baidu.com
  5. target.prototype.path = params;
  6. target.prototype.run = function(){
  7. console.log('我是一个run方法')
  8. }
  9. }
  10. }
  11. //表示把hello赋值给params, 把类赋值给target
  12. @logClass('www.baidu.com')
  13. class HttpClient{
  14. constructor(){ }
  15. getData(){ }
  16. }
  17. var http:any = new HttpClient();
  18. console.log(http.path) //www.baidu.com http.run()

1.3. 类装饰器重载构造函数

类装饰器表达式会在运行时当作函数被调用,类的构造函数作为其唯一的参数 如果类装饰器返回一个值,它会使用提供的构造韩硕来替换类的声明

  1. function logClass(target: any){
  2. console.log(target)
  3. return
  4. class extends target{
  5. apiUrl = '我是修改后的数据';
  6. getData(){
  7. this.apiUrl = this.apiUrl + '12345679';
  8. console.log(this.apiUrl) //我是修改后的数据12345679
  9. }
  10. }
  11. }
  12. @logClass class HttpClient{
  13. public apiUrl : string | undefined;
  14. constructor(){
  15. this.apiUrl = '我是构造函数里面的apiUrl'
  16. }
  17. getData(){
  18. console.log(this.apiUrl)
  19. }
  20. }
  21. var http = new HttpClient();
  22. http.getData()

2. 属性装饰器

定义一个属性装饰器

  1. function logAttr(params: any) {
  2. return function (target: any,attr: any) {
  3. target[attr] = params;
  4. }
  5. }
  6. //调用装饰器
  7. @logClass('www.baidu.com')
  8. class httpClass {
  9. @logAttr('www.taobao.com')
  10. public url: string | undefined;
  11. constructor() { }
  12. getData(){ }
  13. }
  14. let http:any = new httpClass();
  15. console.log(http.path)
  16. console.log(http.add())
  17. console.log(http.url)

3. 方法装饰器

3.1 定义一个方法装饰器

  1. function lgoMethod(params: any) {
  2. return function (target: any, name: any, desc: any) {
  3. console.log(target)
  4. //构造函数 console.log(name)
  5. console.log(desc)
  6. target.url = params; //修改属性的值
  7. target.run = function (target: any) {
  8. console.log('我是一个run方法')
  9. }
  10. }
  11. }
  12. class httpClient {
  13. public url:any | undefined;
  14. constructor() { }
  15. @lgoMethod('www.baidu.com')
  16. getData(){
  17. console.log(this.url)
  18. }
  19. }
  20. const h = new httpClient();
  21. h.getData();
  22. h.run()

3.2 方法装饰器二

定义一个方法装饰器

  1. function classMethod(params: any) {
  2. return function (target: any,name: any, desc: any){
  3. console.log(target)
  4. console.log(name)
  5. console.log(desc) //保存当前方法
  6. var oMethod = desc.value;
  7. console.log(oMethod)
  8. desc.value = function (...args: any[]) {
  9. args = args.map( value => {
  10. return String(value)
  11. })
  12. console.log(args)
  13. oMethod.apply(this,args)
  14. }
  15. }
  16. }
  17. class httpClient {
  18. public url:any | undefined;
  19. constructor() { }
  20. @classMethod('www.jd.com')
  21. getData(...args: any[]){
  22. console.log(args)
  23. console.log('我是getDate方法')
  24. }
  25. }
  26. var htt = new httpClient();
  27. htt.get Data(66,'haha');