TypeScript--装饰器定义,属性装饰器,装饰器工厂

装饰器概念:

  • 装饰器:装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,属性或参数上,可以修改类的行为。
  • 通俗的讲装饰器就是一个方法,可以注入到类、方法、属性参数上来扩展类、属性、方法、参数的功能。
  • 常见的装饰器有:类装饰器、属性装饰器、方法装饰器、参数装饰器
  • 装饰器的写法:普通装饰器(无法传参) 、 装饰器工厂(可传参)
  • 装饰器是过去几年中js最大的成就之一,已是Es7的标准特性之一

1.类装饰器:

类装饰器:类装饰器在类声明之前被声明(紧靠着类声明)。
类装饰器应用于类构造函数,可以用来监视,修改或替换类定义。传入一个参数
要获取装饰器扩展的东西,需要把实例对象的类型设置为any

1.1 类装饰器:普通装饰器(无法传参)

//1.1 类装饰器:普通装饰器(无法传参)
function logClass(params: any) {
    
    
  // params 就是当前类
  console.log(params);
  // 通过原型链扩展属性
  params.prototype.apiUrl = "动态扩展的属性";
  // 通过原型链扩展方法
  params.prototype.run = function () {
    
    
    console.log("我是一个run方法");
  };
}

@logClass
class HttpClient {
    
    
  constructor() {
    
    }
  getData() {
    
    }
}

// 要获取装饰器扩展的东西,需要把实例对象的类型设置为any
var http: any = new HttpClient();

console.log(http.apiUrl);
http.run();

1.2 类装饰器:装饰器工厂(可传参)

function logClass(params: string) {
    
    
  return function (target: any) {
    
    
    target.prototype.apiUrl = params;
  };
}

@logClass("http://www.itying.com/api")
class HttpClient {
    
    
  constructor() {
    
    }
}

var http: any = new HttpClient();

console.log(http.apiUrl);

1.3类装饰器示例

 下面是一个重载构造函数的例子。

 类装饰器表达式会在运行时当作函数被调用,类的构造函数作为其唯一的参数。

 如果类装饰器返回一个值,它会使用提供的构造函数来替换类的声明。
function logClass(target: any) {
    
    
  console.log(target);
  return class extends target {
    
    
    apiUrl: string = "我是修改后的数据";
    getData() {
    
    
      // 这里只能使用this关键字
      this.apiUrl = this.apiUrl + "----";
      console.log(this.apiUrl);
    }
  };
}

@logClass
class HttpClient {
    
    
  public apiUrl: string | undefined;
  constructor() {
    
    
    this.apiUrl = "我是构造函数里面的apiUrl";
  }
  getData() {
    
    
    console.log(this.apiUrl);
  }
}

var http = new HttpClient();
http.getData();

2.属性装饰器

    属性装饰器表达式会在运行时当作函数被调用,传入下列2个参数:
        1、对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
        2、成员的名字。
//属性装饰器
function logProperty(params: any) {
    
    
  return function (target: any, attr: any) {
    
    
    target[attr] = params;
  };
}

class HttpClient {
    
    
  @logProperty("http://itying.com")
  public url: any | undefined;
  constructor() {
    
    }
  getData() {
    
    
    console.log(this.url);
  }
}
var http = new HttpClient();
http.getData();

猜你喜欢

转载自blog.csdn.net/I_r_o_n_M_a_n/article/details/114779267