【NestJS】装饰器


装饰器是一种特殊的类型声明,可以用来修饰:① 类、 ② 属性、 ③ 方法、 ④ 参数


搭建项目:

  1. yarn init -y 初始化项目
  2. yarn add ts-node typescript -D 下载依赖包
  3. tsc --init 初始化 Ts 配置文件
  4. 使用装饰器需要配置 tsconig.json 中的 experimentalDecoratorstrue



类装饰器

装饰器其实就是一函数,接收 class 作为第一参数

/* 创建装饰器 */
const classDecorator: ClassDecorator = target => {
    
    
    console.log('target', target); // target [class A]
    target.prototype.height = 180;
};

/* 使用装饰器 */
@classDecorator
class A {
    
    
    name: string;
    constructor(name: string) {
    
    
        this.name = name;
    }
}

const a = new A('superman');

console.log(a); // A { name: 'superman' }
console.log(Object.getPrototypeOf(a)); // { height: 180 }

ts-node ./XXX.ts 运行 .ts 文件



属性装饰器

属性装饰器接收参数:① 原型对象、 ② 属性名

/* 创建装饰器 */
const propDecorator: PropertyDecorator = (proto, key) => {
    
    
    console.log(proto, key); // {} name
    console.log(proto.constructor); // [class A]
};

class A {
    
    
    /* 使用装饰器 */
    @propDecorator
    name: string;
    constructor(name: string) {
    
    
        this.name = name;
    }
}

const a = new A('superman');



方法装饰器

方法装饰器接收参数:① 原型对象、 ② 方法名、 ③ 属性描述符

/* 创建装饰器 */
const funDecorator: MethodDecorator = (proto, funName, descriptor) => {
    
    
    console.log(proto, funName, descriptor); // {} setName { value: [Function: setName], … }
};

class A {
    
    
    name: string;
    height: number;
    constructor(name: string, height: number) {
    
    
        this.name = name;
        this.height = height;
    }
    /* 使用装饰器 */
    @funDecorator
    setName(name: string, height: number) {
    
    
        this.name = name;
        this.height = height;
    }
}

const a = new A('superman', 180);



参数装饰器

参数装饰器接收参数:① 原型对象、 ② 方法名、 ③ 参数的位置

/* 创建装饰器 */
const paramDecorator: ParameterDecorator = (proto, funName, paramIndex) => {
    
    
    console.log(proto, funName, paramIndex); // {} setName 1
};

class A {
    
    
    name: string;
    height: number;
    constructor(name: string, height: number) {
    
    
        this.name = name;
        this.height = height;
    }
    /* 使用装饰器 */
    setName(name: string, @paramDecorator height: number) {
    
    
        this.name = name;
        this.height = height;
    }
}

const a = new A('superman', 180);



demo

使用装饰器封装 axios.get 请求

yarn add axios

import axios from 'axios';

/* 使用装饰器工厂函数, 以接收装饰器传进来的参数 */
const Get = (url: string): MethodDecorator => {
    
    
    return (proto, key, descriptor: PropertyDescriptor) => {
    
    
        const fnc = descriptor.value; // 表示装饰器所修饰的方法
        axios
            .get(url)
            .then(res => {
    
    
                fnc(res); // 调用方法
            })
            .catch(e => {
    
    
                fnc(e); // 调用方法
            });
    };
};

// 定义控制器
class Controller {
    
    
    constructor() {
    
    }

    @Get('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=2')
    getList({
    
     data }: any) {
    
    
        console.log(data);
    }
}



猜你喜欢

转载自blog.csdn.net/Superman_H/article/details/129097794