相关疑难解决方法(0)

如何在 NEST Js 中使用全局拦截器中的服务

我想在全局拦截器中使用服务。

我的代码如下所示:

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
  }
Run Code Online (Sandbox Code Playgroud)

server.ts 上,我首先是这样初始化的:

app.useGlobalInterceptors(new globalInterceptor())
Run Code Online (Sandbox Code Playgroud)

但是在注入服务后我必须做一些修改,因为现在需要参数 globalInterceptor()

const variableService = await app.get<VariablesService>(VariablesService);
  app.useGlobalInterceptors(new globalInterceptor(variableService));
Run Code Online (Sandbox Code Playgroud)

现在有什么问题是我得到servicenull,我无法创建服务的对象。

GitHub 问题链接

javascript interceptor typescript nestjs

1
推荐指数
1
解决办法
3956
查看次数

标签 统计

interceptor ×1

javascript ×1

nestjs ×1

typescript ×1