美文网首页
TS装饰器

TS装饰器

作者: Water水先生 | 来源:发表于2019-11-06 16:04 被阅读0次

装饰器是一种特殊类型的声明,可以被附加到类生命、方法、访问符、属性或参数上,可以修改类的行为。
使用@expression这种形式,后接函数,在运行时被调用,被装饰的声明信息作为参数传入。

例如:

@Path('/hello')
class HelloSrv {}
在tsconfig中要开启
 { "compilerOptions": {
    "experimentalDecorators" : true
}}

  1. 类装饰器
function Path(path: string) {
    return function (target: Function) {
        !target.prototype.$Meta && 
        (target.prototype.$Meta = {})
            target.prototype.$Meta.baseUrl = path;
    };
}

@Path('/hello')
class HelloService {
    constructor() {}
}

console.log(HelloService.prototype.$Meta);// { baseUrl: '/hello' }
let hello = new HelloService();
console.log(hello.$Meta) // { baseUrl: '/hello' }

  1. 方法装饰器

应用到方法的属性描述符上,用来监视、修改或替换方法定义。

方法装饰会在运行时传入下列3个参数:

  • 对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
  • 成员的名字。
  • 成员的属性描述符。
function GET(url: string) {
    return function (target, methodName: string, descriptor: PropertyDescriptor) {
        !target.$Meta && (target.$Meta = {});
        target.$Meta[methodName] = url;
    }
}

class HelloService {
    constructor() { }
    @GET("xx")
    getUser() { }
}

console.log((<any>HelloService).$Meta);

如果VSCODE编辑报错“作为表达式调用,无法解析方法修饰器的签名”,要在tsconfig中添加
"target": "es6"


  1. 方法参数修饰器
    参数装饰器表达式会在运行时当作函数被调用,传入下列3个参数:
  • 对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
  • 参数的名字。
  • 参数在函数参数列表中的索引。
function PathParam(paramName: string) {
    return function (target, methodName: string, paramIndex: number) {
        !target.$Meta && (target.$Meta = {});
        target.$Meta[paramIndex] = paramName;
    }
}

class HelloService {
    constructor() { }
    getUser( @PathParam("userId") userId: string) { }
}

console.log((<any>HelloService).prototype.$Meta); // {'0':'userId'}

  1. 属性装饰器
    属性装饰器表达式会在运行时当作函数被调用,传入下列2个参数:
  • 对于静态成员来说是类的构造函数,对于实例成员是类的原型对象。
  • 成员的名字。
function DefaultValue(value: string) {
    return function (target: any, propertyName: string) {
        target[propertyName] = value;
    }
}

class Hello {
    @DefaultValue("world") greeting: string;
}

console.log(new Hello().greeting);// 输出: world

装饰器加载顺序

function ClassDecorator() {
    return function (target) {
        console.log("I am class decorator");
    }
}
function MethodDecorator() {
    return function (target, methodName: string, descriptor: PropertyDescriptor) {
        console.log("I am method decorator");
    }
}
function Param1Decorator() {
    return function (target, methodName: string, paramIndex: number) {
        console.log("I am parameter1 decorator");
    }
}
function Param2Decorator() {
    return function (target, methodName: string, paramIndex: number) {
        console.log("I am parameter2 decorator");
    }
}
function PropertyDecorator() {
    return function (target, propertyName: string) {
        console.log("I am property decorator");
    }
}

@ClassDecorator()
class Hello {
    @PropertyDecorator()
    greeting: string;


    @MethodDecorator()
    greet( @Param1Decorator() p1: string, @Param2Decorator() p2: string) { }
}

输出

I am property decorator
I am parameter2 decorator
I am parameter1 decorator
I am method decorator
I am class decorator

属性装饰器 - 方法参数装饰器 - 方法装饰器 - 类装饰器

从上述例子得出如下结论:

1、有多个参数装饰器时:从最后一个参数依次向前执行

2、方法和方法参数中参数装饰器先执行。

3、类装饰器总是最后执行。

4、方法和属性装饰器,谁在前面谁先执行。因为参数属于方法一部分,所以参数会一直紧紧挨着方法执行。上述例子中属性和方法调换位置,输出如下结果:

I am parameter2 decorator
I am parameter1 decorator
I am method decorator
I am property decorator
I am class decorator

参考链接

相关文章

  • 装饰器实验

    装饰器实验 说明 ts内包含了四个装饰器,类装饰器、属性装饰器、函数装饰器、参数装饰器,本文中测试一下其的使用。 ...

  • TS装饰器

    装饰器是一种特殊类型的声明,本质上就是一个方法,可以注入到类、方法、属性、参数上,扩展其功能; 常见的装饰器:类装...

  • TS装饰器

    装饰器是一种特殊类型的声明,可以被附加到类生命、方法、访问符、属性或参数上,可以修改类的行为。使用@express...

  • ts装饰器

    1、装饰器是什么?装饰器是一种特殊类型的声明,他能够附加到类声明、属性、方法、参数上,可以修改类的行为。通俗的讲,...

  • TS装饰器

    一:类的装饰器:是一种与类(class)相关的语法,用来注释或修改类和类方法,装饰器本身是一个函数,装饰器通过@来...

  • 从零开发ts装饰器管理koa路由

    前言 两年前刚学ts,当时搭了个简单的koa的demo,介绍了如何用装饰器管理koa的路由:TS装饰器初体验,用装...

  • TS 装饰器(1): 基础用法

    TS 装饰器(1): 基础用法 1、什么是装饰器 装饰器是通过添加标注的方式,来对类型进行扩展的一种方式。 只能在...

  • ts装饰器写法

    今天做项目的时候发现要用到watch来监听,所以就学习了watch的装饰器写法,然后顺便把之前用过的都看了,这里做...

  • 初识TS装饰器

    写在最前:本文转自掘金[https://juejin.cn/post/7095717238149218317] 前...

  • TS 装饰器(2): 元数据

    TS 装饰器(2): 元数据 在装饰器函数中 ,我们可以拿到类、方法、访问符、属性、参数的基本信息,如它们的名称,...

网友评论

      本文标题:TS装饰器

      本文链接:https://www.haomeiwen.com/subject/fxbubctx.html