像docs这样全局配置缓存之后,如果我在app.module之外使用它,则CacheInterceptor会引发错误。
app.module.ts
const cacheConfig = {
store: redisStore,
host: 'localhost',
port: 6379
}
@Module({
imports: [
CacheModule.register(cacheConfig),
CustomerModule,
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor
}
]
})
export class AppModule {}
customer.module.ts
@Module({
imports: [TypeOrmModule.forFeature([CustomerRepository]), TypeOrmModule.forFeature([User])],
controllers: [CustomerController]
})
export class CustomerModule {}
customer.controller.ts
@Controller('customer')
export class CustomerController {
constructor(
@InjectRepository(CustomerRepository) private customerRepository: CustomerRepository,
@InjectRepository(User) private userRepository: Repository<User>
) {}
@Get()
@UseInterceptors(CacheInterceptor)
async get(): Promise<any> {
const user = await this.userRepository.findOne({ where: { id: 1 }, relations: ['customer'] })
console.log(user.customer.name)
const customer = await this.customerRepository.findOne({ where: { id: 1 }, select: ['id', 'name'] })
return { customer: customer.name, email: user.email }
}
}
我想在所有模块上使用CacheInterceptor而不导入每个CacheModule。
嵌套不能解析APP_INTERCEPTOR的依赖项(UUID:6aa42c77-1bac-4098-b217-1b01eb268240)(?, Reflector)。请确保在CustomerModule上下文中索引[0]处的参数可用。
答案 0 :(得分:0)
如果您有{ provide: APP_INTERCEPTOR, useClass: CacheInterceptor }
,则无需在@UseInterceptors()
的{{1}}装饰器中添加。默认情况下,您应该将controller
与其余设置一起使用