我正在将中间件注入模块中。
致电consumer.apply(middleware)
时遇到类型错误:
代码:
@Module({
...
})
export class FooModule implements NestModule {
@Inject(FOO_MIDDLEWARE)
private middleware: NestMiddleware;
public configure(consumer: MiddlewareConsumer) {
consumer
.apply(this.middleware)
.forRoutes('foo');
}
}
错误:
TS2345: Argument of type 'NestMiddleware<any, any>' is not assignable to parameter of type 'Function | Type<any>'. Type 'NestMiddleware<any, any>' is missing the following properties from type 'Type<any>': apply, call, bind, prototype, and 5 more.
答案 0 :(得分:0)
我找到了解决方法。我做到了:
private middleware: Type<any>
代替:
private middleware: NestMiddleware
从Type
导入@nestjs/common
。