Javascript Typescript装饰程序被调用两次
因为我很确定这是一个bug,但我也想在这里检查一下,以防有人有其他的见解 这就是正在发生的事情 它将记录以下内容:Javascript Typescript装饰程序被调用两次,javascript,typescript,typescript-decorator,Javascript,Typescript,Typescript Decorator,因为我很确定这是一个bug,但我也想在这里检查一下,以防有人有其他的见解 这就是正在发生的事情 它将记录以下内容: Error in /turbo_modules/@fireflysemantics/validator@3.0.10/bundles/fireflysemantics-validator.umd.js (194:21) The ValidationContainer already contains context with signature IsValueIn_Person_n
Error in /turbo_modules/@fireflysemantics/validator@3.0.10/bundles/fireflysemantics-validator.umd.js (194:21)
The ValidationContainer
already contains context with signature IsValueIn_Person_name.
但是如果我们把@IsValueIn(['PETER','JAMES'])注释掉
:
没有例外
当运行时看到并实例化一个decorator时,将调用以下函数(我添加了日志语句,表明decorator被调用了两次):
以下是执行的日志记录语句:
ValidationContainer.ts:69 The target signature is: IsDefined_Person_name
ValidationContainer.ts:68 The property key is: Person_name
ValidationContainer.ts:69 The target signature is: IsAlpha_Person_name
ValidationContainer.ts:68 The property key is: Person_name
ValidationContainer.ts:69 The target signature is: IsValueIn_Person_name
ValidationContainer.ts:68 The property key is: Person_name
ValidationContainer.ts:69 The target signature is: IsValueIn_Person_name
可以看出,IsValueIn\u Person\u name
创建了两次,这是装饰器实例化两次的结果,这会产生异常
想法?基于日志两次调用decorator的假设是错误的 在电流源中:
export function IsValueIn(target: any[], validationOptions?: ValidationOptions) {
const validationParameters:any[] = [];
validationParameters.push(target);
return function(object: any, propertyName: string) {
const vc: ValidationContext = new ValidationContext(
object,
object.constructor,
IsValueIn.name,
propertyName,
validateValue,
null,
true,
errorMessage,
validationOptions
);
ValidationContainer.addValidationContext(vc);
ValidationContainer.addValidationContext(vc);
};
}
ValidationContainer.addValidationContext(vc)
被调用两次并生成这些双日志
把线去掉;) 也许这就是来源:-呼叫两次-没有tsissue@Estradiaz该死,你在我之前就发现了:D
/**
* @param target Add a ValidationContext instance.
* @throws Error if attempting to add a ValidationContext with a signature that duplicates that of an instance already contained.
*
* If an exception thrown it indicates that a duplicate class definition exist
* in the runtime. In other words the same class definition is loaded more
* than once because it exists in multiple files.
*
*/
public static addValidationContext(target: ValidationContext): void {
const key: string = getPropertyKey(
target.target.name,
target.propertyName
);
console.log("The property key is: ", key)
console.log("The target signature is: ", target.getSignature())
ValidationContainer.ts:69 The target signature is: IsDefined_Person_name
ValidationContainer.ts:68 The property key is: Person_name
ValidationContainer.ts:69 The target signature is: IsAlpha_Person_name
ValidationContainer.ts:68 The property key is: Person_name
ValidationContainer.ts:69 The target signature is: IsValueIn_Person_name
ValidationContainer.ts:68 The property key is: Person_name
ValidationContainer.ts:69 The target signature is: IsValueIn_Person_name
export function IsValueIn(target: any[], validationOptions?: ValidationOptions) {
const validationParameters:any[] = [];
validationParameters.push(target);
return function(object: any, propertyName: string) {
const vc: ValidationContext = new ValidationContext(
object,
object.constructor,
IsValueIn.name,
propertyName,
validateValue,
null,
true,
errorMessage,
validationOptions
);
ValidationContainer.addValidationContext(vc);
ValidationContainer.addValidationContext(vc);
};
}