TS中的装饰器

环境:

  1. 安装:node
  2. 安装:typescript
  3. 初始化ts:tsc --init , 自动创建tsconfig.json文件
  4. 修改tsconfig.json中的配置生效:"experimentalDecorators": true,
  5. 安装ts-node:npm i ts-node -g,使用ts-node命令可以直接编译ts文件,不必再转为js
  6. 创建ts文件:index.ts
  7. 编译ts代码:ts-node index.js

1 类装饰器ClassDecorator

  • target是构造函数
// ClassDecorator的类型定义
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
// 类装饰器
const doc :ClassDecorator = (target: Function) => {
    console.log("target==>", target) // target==> [Function: MyClass]
    // 给类中添加一个属性name
    target.prototype.name = "小明"
}

@doc 
class MyClass {
    constructor(){}
}


const myClass:any = new MyClass()
console.log("name===>",myClass.name); //name===> 小明

2. 属性装饰器PropertyDecorator

// 类型定义
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
// 属性 装饰器
const doc :PropertyDecorator = (target: object,key: string|symbol) => {
    console.log("target===>",target,"key===>",key); //target===> {} key===> name
}


class MyClass {
    @doc 
    name:string
    constructor(){
        this.name = "小明"
    }
}

3. 方法装饰器MethodDecorator

declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;

// 方法 装饰器
const doc: MethodDecorator = (
  target: object,
  propertyKey: string | symbol,
  descriptor: any
) => {
  console.log("target===>", target);
  console.log("propertyKey===>", propertyKey);
  console.log("descriptor===>", descriptor);
};

class MyClass {
  constructor() {}
  @doc
  getName() {}
}
/*结果
target===> { getName: [Function (anonymous)] }
propertyKey===> getName	//方法的名字,只是个字符串
descriptor===> {
  value: [Function (anonymous)],
  writable: true,	// 可读
  enumerable: true,	// 可枚举
  configurable: true	// 可配置
}
*/

4. 参数装饰器ParameterDecorator

declare type ParameterDecorator = (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) => void;

// 参数 装饰器
const doc: ParameterDecorator = (
  target: object,
  propertyKey: string | symbol | undefined,
  parameterIndex: number
) => {
  console.log("target===>", target);
  console.log("propertyKey===>", propertyKey);
  console.log("parameterIndex===>", parameterIndex);
};

class MyClass {
  constructor() {}

  getName(name: string, @doc age: number) {}
}
/*
target===> { getName: [Function (anonymous)] }
propertyKey===> getName
parameterIndex===> 1 // 参数索引位置,name是0,age是1
*/

5. 装饰器传参

// 这种写法可能比较好理解
/*
const log = (b: string) => {
  const doc: MethodDecorator = (
    target: any,
    propertyKey: string | symbol,
    descriptor: any
  ) => {
    // getFun就是类中getName方法
    const getFun = descriptor.value;
    getFun(b);
  };
  return doc; // 在函数log中返回另一个函数doc
};
*/


const log = (b: string) => {
  return (target: any, propertyKey: string | symbol, descriptor: any) => {
    // getFun就是类中getName方法
    // 通过descriptor.value获取装饰器装饰的方法
    const getFun = descriptor.value;
    getFun(b);
  };
};

class MyClass {
  constructor() {}
  @log("我是装饰器上的字符串")
  getName(a: string) {
    console.log(a);  // 我是装饰器上的字符串
  }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值