如何在 TypeScript 中实现参数装饰器?
How to implement a parameter decorator in TypeScript?
我一直在尝试使用参数装饰器@logParameter
:
class Person {
public name: string;
public surname: string;
constructor(name : string, surname : string) {
this.name = name;
this.surname = surname;
}
public saySomethingAsync(something: string, @logParameter cb: (said : string) => void) {
cb(this.name + " " + this.surname + " says: " + something);
}
}
我的问题是当我尝试实现装饰器时。 我对目标所做的所有更改都被忽略。
我已经阅读了一些 documentation 并且参数装饰器无法修改目标。
A parameter decorator function is a function that accepts three arguments: The function that contains the decorated parameter, the property key of the member (or undefined for a parameter of the constructor), and the ordinal index of the parameter. The return value of this decorator is ignored.
文档说我可以使用参数装饰器来注释
annotate the target and index
然后使用方法装饰器读取注解。它会是这样的:
@readParameterAnnotations
public saySomethingAsync(something: string, @logParameter cb: (said : string) => void) {
cb(this.name + " " + this.surname + " says: " + something);
}
我的问题是,如果目标的所有更改都被忽略,我不明白如何向目标添加注释?
function logParameter(target: any, key: string, index: number) {
// how can I annotate the target?
}
使用 reflect-metadata 是唯一的方法吗?
function logParameter(target: any, key: string, index: number) {
Reflect.defineMetadata("log_parameters", index, target, key);
}
The return value of this decorator is ignored.
这与您的情况无关。参数装饰器的 return 值被忽略,因为它们不需要能够替换任何东西(不像方法和 class 装饰器可以替换描述符)。
My problem is that when I try to implement the decorator. All the changes that I do to the target are ignored.
目标是对象的原型。它工作正常:
class MyClass {
myMethod(@logParam myParameter: string) {}
}
function logParam(target: any, methodKey: string, parameterIndex: number) {
target.test = methodKey;
// and parameterIndex says which parameter
}
console.log(MyClass.prototype["test"]); // myMethod
var c = new MyClass();
console.log(c["test"]); // myMethod
只需改变这种情况,将数据放在您想要的位置(使用 Reflect.defineMetadata
可能是最好的)。
我一直在尝试使用参数装饰器@logParameter
:
class Person {
public name: string;
public surname: string;
constructor(name : string, surname : string) {
this.name = name;
this.surname = surname;
}
public saySomethingAsync(something: string, @logParameter cb: (said : string) => void) {
cb(this.name + " " + this.surname + " says: " + something);
}
}
我的问题是当我尝试实现装饰器时。 我对目标所做的所有更改都被忽略。
我已经阅读了一些 documentation 并且参数装饰器无法修改目标。
A parameter decorator function is a function that accepts three arguments: The function that contains the decorated parameter, the property key of the member (or undefined for a parameter of the constructor), and the ordinal index of the parameter. The return value of this decorator is ignored.
文档说我可以使用参数装饰器来注释
annotate the target and index
然后使用方法装饰器读取注解。它会是这样的:
@readParameterAnnotations
public saySomethingAsync(something: string, @logParameter cb: (said : string) => void) {
cb(this.name + " " + this.surname + " says: " + something);
}
我的问题是,如果目标的所有更改都被忽略,我不明白如何向目标添加注释?
function logParameter(target: any, key: string, index: number) {
// how can I annotate the target?
}
使用 reflect-metadata 是唯一的方法吗?
function logParameter(target: any, key: string, index: number) {
Reflect.defineMetadata("log_parameters", index, target, key);
}
The return value of this decorator is ignored.
这与您的情况无关。参数装饰器的 return 值被忽略,因为它们不需要能够替换任何东西(不像方法和 class 装饰器可以替换描述符)。
My problem is that when I try to implement the decorator. All the changes that I do to the target are ignored.
目标是对象的原型。它工作正常:
class MyClass {
myMethod(@logParam myParameter: string) {}
}
function logParam(target: any, methodKey: string, parameterIndex: number) {
target.test = methodKey;
// and parameterIndex says which parameter
}
console.log(MyClass.prototype["test"]); // myMethod
var c = new MyClass();
console.log(c["test"]); // myMethod
只需改变这种情况,将数据放在您想要的位置(使用 Reflect.defineMetadata
可能是最好的)。