【发布时间】:2018-11-03 08:46:27
【问题描述】:
我想在全局拦截器中使用服务。
我的代码如下所示:
import { VariablesService } from '../app/modules/variables/variables.service';
@Interceptor()
export class globalInterceptor implements NestInterceptor {
constructor(private service: VariablesService) {
console.log('contructor running', service); //getting null here
}
在 server.ts 上,我首先是这样初始化的:
app.useGlobalInterceptors(new globalInterceptor())
但是在注入服务之后我必须做一些修改,因为globalInterceptor()中现在需要参数
const variableService = await app.get<VariablesService>(VariablesService);
app.useGlobalInterceptors(new globalInterceptor(variableService));
现在问题是我得到的service 是null,我无法创建服务对象。
【问题讨论】:
标签: javascript typescript interceptor nestjs