一、方法装饰器

  • 它会被应用到方法的属性描述符上,可以用来监视,修改或者替换方法定义
  • 方法装饰会在运行时传入下列3个参数:
  • 1、对于静态成员来说是类的构造函数,对于实例成员是类的原型对象
  • 2、成员的名字
  • 3、成员的属性描述符

1、方法装饰器一
  1. function get(params: any) {
  2. return function (target: any, methodName: any, desc: any) {
  3. console.log(target);
  4. console.log(methodName);
  5. console.log(desc);
  6. target.apiUrl = 'xxxx';
  7. target.run = function () {
  8. console.log('run');
  9. }
  10. }
  11. }
  12. class HttpClient {
  13. public url: any | undefined;
  14. constructor() {
  15. }
  16. @get('http://www.itying,com')
  17. getData() {
  18. console.log(this.url);
  19. }
  20. }
  21. var http: any = new HttpClient();
  22. console.log(http.apiUrl);
  23. http.run();

2、方法装饰器二
  1. function get(params: any) {
  2. return function (target: any, methodName: any, desc: any) {
  3. console.log(target);
  4. console.log(methodName);
  5. console.log(desc.value);
  6. // 修改装饰器的方法,把装饰器方法里面传入的所有参数改为string类型
  7. // 1、保存当前的方法、修改getData方法
  8. var oMethod = desc.value;
  9. desc.value = function (...args: any[]) {
  10. args = args.map((value) => {
  11. return String(value);
  12. })
  13. oMethod.apply(this, args);
  14. }
  15. }
  16. }
  17. class HttpClient {
  18. public url: any | undefined;
  19. constructor() {
  20. }
  21. @get('http://www.itying,com')
  22. getData(...args: any[]) {
  23. console.log(args);
  24. console.log('我是getData里面的方法');
  25. }
  26. }
  27. var http = new HttpClient();
  28. http.getData(123, 'xxx');

3、方法参数装饰器
  • 参数装饰器表达式会在运行时当作函数被调用,可以使用参数装饰器为类的原型增加一些元素数据 ,传入下列3个参数:
  • 1、对于静态成员来说是类的构造函数,对于实例成员是类的原型对象
  • 2、方法的名字
  • 3、参数在函数参数列表中的索引 ```typescript function logParams(params: any) { return function (target: any, methodName: any, paramsIndex: any) {
    1. console.log(params);
    2. console.log(target);
    3. console.log(methodName);
    4. console.log(paramsIndex);
    5. target.apiUrl = params;
    } }

class HttpClient { public url: any | undefined; constructor() { } getData(@logParams(‘xxxxx’) uuid: any) { console.log(uuid); } }

var http: any = new HttpClient(); http.getData(123456); console.log(http.apiUrl);

  1. <a name="db2226be"></a>
  2. ##### 4、装饰器执行顺序
  3. - 属性》方法》方法参数》类
  4. - 如果有多个同样的装饰器,它会先执行后面的
  5. ```typescript
  6. function logClass1(params: string) {
  7. return function (target: any) {
  8. console.log('类装饰器1')
  9. }
  10. }
  11. function logClass2(params: string) {
  12. return function (target: any) {
  13. console.log('类装饰器2')
  14. }
  15. }
  16. function logAttribute1(params?: string) {
  17. return function (target: any, attrName: any) {
  18. console.log('属性装饰器1')
  19. }
  20. }
  21. function logAttribute2(params?: string) {
  22. return function (target: any, attrName: any) {
  23. console.log('属性装饰器2')
  24. }
  25. }
  26. function logMethod1(params?: string) {
  27. return function (target: any, attrName: any, desc: any) {
  28. console.log('方法装饰器1')
  29. }
  30. }
  31. function logMethod2(params?: string) {
  32. return function (target: any, attrName: any, desc: any) {
  33. console.log('方法装饰器2')
  34. }
  35. }
  36. function logParams1(params?: string) {
  37. return function (target: any, attrName: any, desc: any) {
  38. console.log('方法参数装饰器1')
  39. }
  40. }
  41. function logParams2(params?: string) {
  42. return function (target: any, attrName: any, desc: any) {
  43. console.log('方法参数装饰器2')
  44. }
  45. }
  46. // 调用装饰器部分
  47. @logClass1('http://www.itying.com/api')
  48. @logClass2('xxxx')
  49. class HttpClient {
  50. @logAttribute1()
  51. @logAttribute2()
  52. public apiUrl: string | undefined;
  53. constructor() {
  54. }
  55. @logMethod1()
  56. @logMethod2()
  57. getData() {
  58. return true;
  59. }
  60. setData(@logParams1() attr1: any, @logParams2() attr2: any,) {
  61. }
  62. }
  63. var http: any = new HttpClient();