typescript - '巢中间件' 不能分配给 'Function | 类型的参数 类型'
问题描述
我正在将中间件注入到模块中。
我打电话时遇到类型错误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.
解决方案
我找到了解决方法。我做了:
private middleware: Type<any>
代替:
private middleware: NestMiddleware
Type
从导入@nestjs/common
。
推荐阅读
- java - Map Reduce - 如何在单个作业中对多个属性进行分组和聚合
- javascript - 使用 SignalR 和 Clients.All.myJsfunction 从 C# 触发 JS 函数
- machine-learning - Keras 实现问题出乎意料的关键字参数“轴”
- python - 加速 sympy 矩阵运算
- jquery - jQuery - slice() 函数在 Microsoft Edge 上从 1 而不是 0 开始计数
- ruby-on-rails - API 请求不与 rest-client 一起使用,但与 Faraday/cURL 一起使用
- javascript - 事件循环更喜欢微任务队列而不是回调队列?
- c++ - 如何调试 bazel 编译 C++ 代码?
- php - 选择框的组件在 laravel 中的视图格式中不起作用
- php - 下载时传输的 fopen 字节