我在 nest 中有一个全局记录器模块,它记录到云日志服务。我正在尝试创建一个添加日志记录功能的类方法装饰器。但是我正在努力如何在装饰器中注入全局嵌套模块的服务,因为我在文档中找到的所有依赖注入机制依赖都是基于类或类属性的注入。
export function logDecorator() {
// I would like to inject a LoggerService that is a provider of a global logger module
let logger = ???
return (target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) => {
//get original method
const originalMethod = propertyDescriptor.value;
//redefine descriptor value within own function block
propertyDescriptor.value = function(...args: any[]) {
logger.log(`${propertyKey} method called with args.`);
//attach original method implementation
const result = originalMethod.apply(this, args);
//log result of method
logger.log(`${propertyKey} method return value`);
};
}; …Run Code Online (Sandbox Code Playgroud) nestjs ×1