我创建了一个包含服务的核心模块。
import { Module } from '@nestjs/common';
import { CacheService } from './cache.service';
@Module({
providers:[{ provide: CacheService, useValue: new CacheService()}],
exports:[{ provide: CacheService, useValue: new CacheService()}]
})
export class CoreModule {}
我已将CoreModule
导入了AppModule
。
@Module({
imports: [
TypeOrmModule.forRoot(typeOrmConfig),
CoreModule,
offerModule
],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
在AppModule
中导入了一个报价模块。当我尝试将CacheService
插入checkmodule时,它会在下面抛出错误。
2019-02-05 13:40:25 [error]: Error: Nest can't resolve dependencies of the OfferService (OfferRepository, ?, Connection, AppLogger). Please make sure that the argument at index [1] is available in the OfferModule context.
at Injector.lookupComponentInExports (C:\Project Workspace\ts-sharing\node_modules\@nestjs\core\injector\injector.js:144:19)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
at Function.Module.runMain (module.js:696:11)
at Object.<anonymous> (C:\Project Workspace\ts-sharing\node_modules\ts-node\src\bin.ts:157:12)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
1: v8::internal::compiler::Type::Negative31
2: v8::internal::wasm::SignatureMap::Find
3: v8::internal::Builtins::CallableFor
4: v8::internal::Builtins::CallableFor
5: v8::internal::Builtins::CallableFor
6: 0000023CE08843C1
有人可以帮忙吗?
答案 0 :(得分:1)
您还必须在CoreModule
中导入OfferModule
,以便提供者可以使用它。
如果您希望CoreModule
在全球范围内可用,可以使用@Global
装饰器将其设置为global module。有了它,该模块只需导入一次(例如,在AppModule
中),然后在所有模块中都可用。
@Global()
@Module({
providers:[CacheService],
exports:[CacheService],
})
export class CoreModule {}