【发布时间】:2020-07-10 16:00:15
【问题描述】:
我猜是因为我是用翻译功能写句子的,所以很难读,对不起。
我正在尝试基于接口注入提供程序。 下面是我的代码。
storage-base.interface.ts
export interface IStorageBase {
upload(fileName: string): Promise<string>;
deleteLocalFile(fileName: string): Promise<void>;
}
blob.service.ts
import { Injectable } from '@nestjs/common';
import { BlobServiceClient, ContainerClient } from '@azure/storage-blob';
import { IStorageBase } from './storage-base.interface';
type BlobDependencies = {
connectionString: string;
containerName: string;
};
@Injectable()
export class BlobService implements IStorageBase {
private readonly blobServiceClient: BlobServiceClient;
private readonly containerClient: ContainerClient;
constructor(blobDependencies: BlobDependencies) {
//Some kind of processing
}
upload(filename: string): Promise<string> {
//Some kind of processing
}
deleteLocalFile(filename: string): Promise<void> {
//Some kind of processing
}
private getAbsolutePath(filename: string): string {
//Some kind of processing
}
}
core.module.ts
import { Module } from '@nestjs/common';
import { BlobService } from './blob.service';
const blobServiceProvider = { provide: 'IStorageBase', useClass: BlobService };
@Module({
providers: [blobServiceProvider],
exports: [blobServiceProvider],
})
export class CoreModule {}
Blob 类实现了IStorageBase 接口。
而CoreModule 使基于IStoraegBase 的提供程序可用于其他模块。
示例:
video-cut-out.module.ts
import { Module } from '@nestjs/common';
import { VideoCutOutService } from './video-cut-out.service';
import { VideoCutOutController } from './video-cut-out.controller';
import { CoreModule } from '../core/core.module';
@Module({
imports: [CoreModule],
providers: [VideoCutOutService],
controllers: [VideoCutOutController],
exports: [],
})
export class VideoCutOutModule {}
video-cut-out.service.ts
import { Injectable, Inject } from '@nestjs/common';
import { VideoCutOutRequest, VideoCutOutResponse } from './video-cut-out.model';
import { IStorageBase } from '../core/storage-base.interface';
@Injectable()
export class VideoCutOutService {
constructor(@Inject('IStorageBase') private _blob: IStorageBase) {}
async executeAsync(
videoCutOutRequest: VideoCutOutRequest,
): Promise<VideoCutOutResponse> {
//Some kind of processing
}
}
但是,执行此操作时,会输出以下错误。
[Nest] 18352 - 2020-03-30 17:04:12 [ExceptionHandler] Nest can't resolve dependencies of the IStorageBase (?). Please make sure that the argument Object at index [0] is available in the CoreModule context.
Potential solutions:
- If Object is a provider, is it part of the current CoreModule?
- If Object is exported from a separate @Module, is that module imported within CoreModule?
@Module({
imports: [ /* the Module containing Object */ ]
})
+47ms
我认为该接口不能注入到模块装饰器的“提供者”或“导入”中。 如何解决上述错误并正常运行程序?
谢谢。
【问题讨论】:
标签: nestjs