环回 4 - 无法从使用 @inject.tag() 注入的 类 数组中获取元数据
Loopback 4 - Not able to get metadata from array of classes injected with @inject.tag()
有几个 classes 注册为服务:
@processor('some_value')
export class MyService {}
@processor('another_value')
export class AnotherService {}
此服务使用 processor() 装饰器装饰,定义如下:
const PROCESSOR_CLASS_META = MetadataAccessor.create<
string,
ClassDecorator
>('processor-decorator-metadata');
export function processor(value): ClassDecorator {
return (
target: Function & {value?: string},
) => {
bind({tags: 'processor'})(target);
const decorator = ClassDecoratorFactory.createDecorator(
PROCESSOR_CLASS_META,
value,
{decoratorName: '@processor'},
);
decorator(target);
};
}
装饰器做两件事 - 将 class 标记为 'processor' 并设置 string
类型元数据。
现在我有了第三个服务。在此服务中,我注入所有标记为 'processor' 的服务,如下所示:
export class consumerService {
constructor(
@inject.tag('processor')
private readonly processorClasses: Constructor<Function>,
)
}
当我尝试获取注入的 classes 的元数据时,我得到 undefined
:
export class consumerService {
constructor(
@inject.tag('processor')
private readonly processorClasses: Constructor<Function>,
)
public getMetaData() {
return this.processorClasses.map((processorClass) => {
// Returns undefined
return MetadataInspector.getClassMetadata(
PROCESSOR_CLASS_META,
processorClass,
);
})
}
}
undefined
只有在我使用 @inject.tag() 装饰器注入绑定数组时才会出现。
当我只注入 classes 之一时,我得到了预期的结果:
export class consumerService {
constructor(
@inject('services.MyService')
private readonly myService: MyService,
)
// Returns 'some_value' as expected.
public getMetaData() {
return MetadataInspector.getClassMetadata(
PROCESSOR_CLASS_META,
this.myService,
);
}
}
装饰器 @inject.tag()
正在注入服务 实例 ,而不是 classes。您可以通过 .constructor
属性.
访问 class (构造函数)
这是您的 ConsumerService
实施的固定版本:
class ConsumerService {
constructor(
@inject.tag('processor')
private readonly processors: object[],
) {
console.log('processors', this.processors);
for (const proc of this.processors) {
console.log(
it.constructor.name,
MetadataInspector.getClassMetadata(
PROCESSOR_CLASS_META,
proc.constructor,
),
);
}
}
}
当我 运行 使用您的示例处理器时,我得到以下输出:
processors [ MyService {}, AnotherService {} ]
MyService some_value
AnotherService another_value
有几个 classes 注册为服务:
@processor('some_value')
export class MyService {}
@processor('another_value')
export class AnotherService {}
此服务使用 processor() 装饰器装饰,定义如下:
const PROCESSOR_CLASS_META = MetadataAccessor.create<
string,
ClassDecorator
>('processor-decorator-metadata');
export function processor(value): ClassDecorator {
return (
target: Function & {value?: string},
) => {
bind({tags: 'processor'})(target);
const decorator = ClassDecoratorFactory.createDecorator(
PROCESSOR_CLASS_META,
value,
{decoratorName: '@processor'},
);
decorator(target);
};
}
装饰器做两件事 - 将 class 标记为 'processor' 并设置 string
类型元数据。
现在我有了第三个服务。在此服务中,我注入所有标记为 'processor' 的服务,如下所示:
export class consumerService {
constructor(
@inject.tag('processor')
private readonly processorClasses: Constructor<Function>,
)
}
当我尝试获取注入的 classes 的元数据时,我得到 undefined
:
export class consumerService {
constructor(
@inject.tag('processor')
private readonly processorClasses: Constructor<Function>,
)
public getMetaData() {
return this.processorClasses.map((processorClass) => {
// Returns undefined
return MetadataInspector.getClassMetadata(
PROCESSOR_CLASS_META,
processorClass,
);
})
}
}
undefined
只有在我使用 @inject.tag() 装饰器注入绑定数组时才会出现。
当我只注入 classes 之一时,我得到了预期的结果:
export class consumerService {
constructor(
@inject('services.MyService')
private readonly myService: MyService,
)
// Returns 'some_value' as expected.
public getMetaData() {
return MetadataInspector.getClassMetadata(
PROCESSOR_CLASS_META,
this.myService,
);
}
}
装饰器 @inject.tag()
正在注入服务 实例 ,而不是 classes。您可以通过 .constructor
属性.
这是您的 ConsumerService
实施的固定版本:
class ConsumerService {
constructor(
@inject.tag('processor')
private readonly processors: object[],
) {
console.log('processors', this.processors);
for (const proc of this.processors) {
console.log(
it.constructor.name,
MetadataInspector.getClassMetadata(
PROCESSOR_CLASS_META,
proc.constructor,
),
);
}
}
}
当我 运行 使用您的示例处理器时,我得到以下输出:
processors [ MyService {}, AnotherService {} ]
MyService some_value
AnotherService another_value