简体   繁体   English

Typescript 装饰器参数

[英]Typescript decorators parameters

I would like to understand what are the different parameters of a Typescript decorator.我想了解 Typescript 装饰器的不同参数是什么。

 function myDecorator(target) { // do something with 'target'... }

In the example above, I know that target represents the function/class the decorator is attached to, but what are the other parameters of a decorator, their meaning an order, I also would like to get a link to an official documentation specifying this.在上面的示例中,我知道 target 表示装饰器附加到的函数/类,但是装饰器的其他参数是什么,它们的含义是一个命令,我还想获得一个官方文档的链接来指定这个。

Thanks in advance.提前致谢。

A decorator expects one argument, the target it decorates and some more arguments depending on the type of the target, eg装饰器需要一个参数,它装饰的目标和更多 arguments 取决于目标的类型,例如

Method Decorators方法装饰器

The expression for the method decorator will be called as a function at runtime, with the following three arguments:方法装饰器的表达式在运行时将被称为 function,其中包含以下三个 arguments:

  • Either the constructor function of the class for a static member, or the prototype of the class for an instance member. static 成员的 class 的构造函数 function 或成员实例的 ZA2F2ED4F8EBC2CBBD4C21 的原型
  • The name of the member.成员的姓名。
  • The Property Descriptor for the member.成员的属性描述符。

You can find the complete lists on TypeScript Decorators for all decorator types:您可以在TypeScript 装饰器上找到所有装饰器类型的完整列表:

  • Class Decorators Class 装饰器

    • Only target class仅针对 class
  • Method Decorators方法装饰器

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member, static 成员的 class 的构造函数 function 或成员的实例 ZA2F2ED4F8EBC2CBBDZC21 的原型
    • The name of the member,会员姓名,
    • The Property Descriptor for the member成员的属性描述符
  • Accessor Decorators访问器装饰器

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member. static 成员的 class 的构造函数 function 或成员实例的 ZA2F2ED4F8EBC2CBBD4C21 的原型
    • The name of the member.成员的姓名。
    • The Property Descriptor for the member.成员的属性描述符。
  • Property Decorators物业装饰师

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member. static 成员的 class 的构造函数 function 或成员实例的 ZA2F2ED4F8EBC2CBBD4C21 的原型
    • The name of the member.成员的姓名。
  • Parameter Decorators参数装饰器

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member. static 成员的 class 的构造函数 function 或成员实例的 ZA2F2ED4F8EBC2CBBD4C21 的原型
    • The name of the member.成员的姓名。
    • The ordinal index of the parameter in the function's parameter list.函数参数列表中参数的序号索引。

Additionally:此外:

If we want to customize how a decorator is applied to a declaration, we can write a decorator factory.如果我们想自定义如何将装饰器应用于声明,我们可以编写一个装饰器工厂。 A Decorator Factory is simply a function that returns the expression that will be called by the decorator at runtime.装饰器工厂只是一个 function ,它返回将由装饰器在运行时调用的表达式。

[ TypeScript Decorators ] [ TypeScript 装饰器]

You can create decorator factories with parameters as you need.您可以根据需要创建带有参数的装饰器工厂。 They're not limited or specified.它们不受限制或指定。

The examples from TypeScript documentation. TypeScript 文档中的示例。

Decorator:装饰师:

function sealed(target) {
  // do something with 'target' ...
}

applied with适用于

@sealed x

Decorator factory:装饰厂:

function color(value: string) {
  // this is the decorator factory
  return function (target) {
    // this is the decorator
    // do something with 'target' and 'value'...
  };
}

applied as应用为

@color('blue') x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM