:::info 装饰器:装饰器是一种特殊类型的声明、它能够被附加到类声明、方法、属性、参数的功能;
理解:装饰器就是一个方法,可以注入到类、方法、属性、参数上来拓展类、属性、方法、参数的功能
常见装饰器:类装饰器、属性装饰器、方法装饰器、参数装饰器
装饰器的写法:普通装饰器(无法传参)、装饰器工厂(可传参)
装饰器是过去几年js中的重大成就,已经是ES7的标准特性之一 :::

类装饰器

基础装饰器
  1. // 装饰器
  2. function logClass (params:any) {
  3. console.log(params);
  4. params.prototype.apiUrl = '动态扩展参数';
  5. params.prototype.run = function () {
  6. console.log('动态扩展方法')
  7. }
  8. }
  9. // 类装饰器
  10. @logClass
  11. class HttpClient {
  12. constructor () {
  13. }
  14. getData () {
  15. }
  16. }
  17. var http:any = new HttpClient();
  18. console.log(http.apiUrl);
  19. console.log(http.run());

类装饰器构造工厂(可传参)
  1. // 装饰器
  2. function logClass(params: string) {
  3. return function (target:any) {
  4. console.log(target)
  5. console.log(params)
  6. target.prototype.apiUrl = params;
  7. }
  8. }
  9. // 类装饰器
  10. @logClass('http://www.baidu.com')
  11. class HttpClient {
  12. constructor() {
  13. }
  14. getData() {
  15. }
  16. }
  17. var http:any = new HttpClient();
  18. console.log(http.apiUrl)

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

属性装饰器

:::success 属性装饰器表达式在运行时被当作函数调用,需要传递两个参数
参数1:对于静态成员来说是类的构造函数、对于实例成员来说是类的原型对象
参数2:成员的名称 :::

  1. function logProperty (params:any) {
  2. return function (target:any,attr:any) {
  3. console.log(target) // {constructor: ƒ, getData: ƒ}
  4. console.log(attr) // apiUrl
  5. target[attr] = 'http://qdclub.com.cn'
  6. }
  7. }
  8. class HttpRequest {
  9. @logProperty('http://www.baidu.com')
  10. public apiUrl: string | undefined;
  11. constructor() {
  12. }
  13. getData() {
  14. console.log(this.apiUrl);
  15. }
  16. }
  17. var http:any = new HttpRequest();
  18. http.getData();

方法装饰器

:::success 方法装饰器会被应用到方法的属性描述上,可以用来监视、修改或替换方法定义
方法装饰器运行时需要传入三个参数:

  • 参数1:对于静态成员来说是类的构造函数,对于实例成员来说是类的原型对象
  • 参数2:成员的名称
  • 参数3:成员的属性描述符 ::: ```javascript function logFunction (params: any) { console.log(params) return function (target:any, methodsName:any, desc:any) {
    1. console.log(target) // {constructor: ƒ, getData: ƒ}
    2. console.log(methodsName) // getData
    3. console.log(desc) // {writable: true, enumerable: false, configurable: true, value: ƒ}
    4. target.apiUrl = 'XXX';
    5. target.run = function () {
    6. console.log('run');
    7. }
    8. // 修改装饰器的方法
    9. // 1.保存当前的方法
    10. var oMethods = desc.value;
    11. desc.value = function(...args:any[]) {
    12. args = args.map((value) => {
    13. return String(value)
    14. })
    15. console.log(args);
    16. oMethods.apply(this, args);
    17. }
    } }

class HttpRequest { public url: string | undefined; constructor() { } @logFunction(‘http://www.baidu.com‘) getData() { console.log(‘我是构造器中的方法’) } } var http:any = new HttpRequest(); http.getData(‘123’, 123); // [‘123’, ‘123’] console.log(http.apiUrl); http.run();

  1. <a name="TrYgH"></a>
  2. ##### 方法参数装饰器
  3. :::success
  4. 参数装饰器会在运行时当作函数调用,可以使用参数装饰器为类的原型增加一些元素数据,传入下列3个参数:
  5. - 参数1:对于静态成员来说是类的构造函数,对于实例成员来说是类的原型对象
  6. - 参数2:方法的名称
  7. - 参数3:参数在函数参数列表中的索引
  8. :::
  9. ```javascript
  10. function logParams (params:any) {
  11. return function (target: any, methodsName: any, paramsIndex: any) {
  12. console.log(target)
  13. console.log(methodsName)
  14. console.log(paramsIndex)
  15. target.apiUrl = params
  16. }
  17. }
  18. class HttpRequest {
  19. public url: string | undefined;
  20. constructor() {
  21. }
  22. getParams() {}
  23. getData(name:string, @logParams('uuid') uuid:any) {
  24. console.log('我是构造器中的方法')
  25. }
  26. }
  27. var http:any = new HttpRequest();
  28. http.getData('12123',123123);
  29. console.log(http.apiUrl);

:::danger 装饰器执行的顺序
属性装饰器 > 方法参数装饰器(如有多个从后到前)> 方法装饰器 > 类装饰器(如有多个从后到前) :::

  1. // 类装饰器
  2. function leiClass1 (params:any) {
  3. return function (target:any) {
  4. console.log('类装饰器1')
  5. }
  6. }
  7. function leiClass2(params: any) {
  8. return function (target: any) {
  9. console.log('类装饰器2')
  10. }
  11. }
  12. // 属性构造器
  13. function logParams1 (params:any) {
  14. return function (target:any, paramsName: any) {
  15. console.log('属性装饰器1')
  16. }
  17. }
  18. function logParams2(params: any) {
  19. return function (target: any, paramsName: any) {
  20. console.log('属性装饰器2')
  21. }
  22. }
  23. // 方法构造器
  24. function logFunction1 (params:any) {
  25. return function (target:any, methodsName:any, desc:any) {
  26. console.log('方法装饰器1')
  27. }
  28. }
  29. function logFunction2(params: any) {
  30. return function (target: any, methodsName: any, desc: any) {
  31. console.log('方法装饰器2')
  32. }
  33. }
  34. // 方法参数构造器
  35. function logFunParams1 (params:any) {
  36. return function (target:any,methodsName:any,paramsIndex:any) {
  37. console.log('方法参数构造器1')
  38. }
  39. }
  40. function logFunParams2(params: any) {
  41. return function (target: any, methodsName: any, paramsIndex: any) {
  42. console.log('方法参数构造器2')
  43. }
  44. }
  45. @leiClass1('http://www.baicu.com')
  46. @leiClass2('http://www.baicu.com')
  47. class HttpClient {
  48. @logParams1('12331')
  49. public apiUlr: string | undefined
  50. @logParams2('123321')
  51. public apiParams: string | undefined
  52. @logFunction1('123')
  53. getData(@logFunParams1('uuid') uuid: any, @logFunParams2('uuid2') uuid2:any) {}
  54. @logFunction2('123')
  55. setData () {}
  56. }
  57. //属性装饰器1
  58. //属性装饰器2
  59. //方法参数构造器2
  60. //方法参数构造器1
  61. //方法装饰器1
  62. //方法装饰器2
  63. //类装饰器2
  64. //类装饰器1