Nest.js学习笔记(三)

简介: 本节记录TypeScript的若干装饰器,仅做示范,如需详细了解可自行移步官方文档
// 普通类装饰器(不可传参)
const doc: ClassDecorator = (target: any) => {
    console.log(target)
}

@doc
class Xiaoman {
    constructor() {}
}

// 类工厂装饰器(可自由传参)
const docs = (str: string): ClassDecorator => {
    return (target: any): void => {
        console.log(str)
    }
}

@docs('这是一段字符串')
class XiaomanFactory {
    constructor() {}
}

// 属性装饰器(返回构造函数和属性名)
const props: PropertyDecorator = (target: any, key: string | symbol) => {
    console.log(target, key)
}

class XiaomanProperty {
    @props
    public name: string
    constructor() {
        this.name = '小满'
    }
}

// 方法装饰器(返回构造函数、方法名和方法定义)
const method:MethodDecorator = (target: any, propertyKey: string | symbol,descriptor:any) => {
    console.log(target,propertyKey,descriptor);
}

class XiaomanMethod {
    constructor(){}
    @method
    getName(){}
}

// 参数装饰器(返回构造函数、参数名和参数所在位置(从前往后递减))
const param:ParameterDecorator = (target:any,propertyKey: string | symbol,parameterIndex: number) => {
    console.log(target,propertyKey,parameterIndex)
}

class XiaomanParams {
    constructor(){}
    myMethod(@param data:any,@param key:any){}
}

// 装饰器底层实现
class CustomXiaoman {
    constructor(){}
}
const custom = (target:any) => {
    console.log(target)
}
custom(CustomXiaoman)
相关文章
快速入门nest.js(7/10)--应用配置
然后,你就可以像下面的方式使用对应的变量值了,注意这里所有的变量值都是字符串,而port要求的是数字,所以我们还需要进行一个转换。
128 0
|
3天前
|
中间件
12_nest.js中间
12_nest.js中间
39 1
|
3天前
11_nest.js模块
11_nest.js模块
25 0
|
9月前
|
API 网络架构
初识nest.js的controller(入门)
初识nest.js的controller(入门)
|
10月前
|
JSON 前端开发 JavaScript
NEST.JS使用心得
NEST.JS使用心得
|
10月前
|
SQL JSON JavaScript
为什么从egg.js到nest.js(一)
进入部门工作后,接触到的node.js服务端框架,是egg.js,后面基于扩展增加了很多插件,比如:@Controller @Service等注解,还有针对egg-framework 定制化部门使用的底层framework。
350 0
|
10月前
|
JavaScript
为什么从egg.js到nest.js(二)
进入部门工作后,接触到的node.js服务端框架,是egg.js,后面基于扩展增加了很多插件,比如:@Controller @Service等注解,还有针对egg-framework 定制化部门使用的底层framework。
79 0
为什么从egg.js到nest.js(二)
|
JavaScript 前端开发
Nest.js学习笔记(七)
本节详细介绍Nest.js的控制器,以及使用Apifox做了简单的测试
234 0
|
容器
Nest.js学习笔记(二)
本节记录依赖倒置、控制反转、依赖注入相关内容
60 0
|
API
Nest.js学习笔记(六)
本节记录Restful风格Api的介绍以及使用Nest.js做了一个简单的版本控制
117 0