1
DIを介してnestjsのサードパーティライブラリをインポートする方法を理解したいと思います。だから、私はクラスAuthService
を持っている:nest.jsでDI問題を解決するには?
export class AuthService {
constructor(
@Inject(constants.JWT) private jsonWebToken: any,
){}
....
}
JWTプロバイダ:
import * as jwt from 'jsonwebtoken';
import {Module} from '@nestjs/common';
import constants from '../../../constants';
const jwtProvider = {
provide: constants.JWT,
useValue: jwt,
};
@Module({
components: [jwtProvider],
})
export class JWTProvider {}
ライブラリモジュール:
import { Module } from '@nestjs/common';
import {BcryptProvider} from './bcrypt/bcrypt.provider';
import {JWTProvider} from './jsonwebtoken/jwt.provider';
@Module({
components: [
BcryptProvider,
JWTProvider,
],
controllers: [],
exports: [
BcryptProvider,
JWTProvider,
],
})
export class LibrariesModule{
}
私はこのエラーを取得しています:
Error: Nest can't resolve dependencies of the AuthService (?). Please verify whether [0] argument is available in the current context.
at Injector.<anonymous> (D:\Learning\nest\project\node_modules\@nestjs\core\injector\injector.js:156:23)
at Generator.next (<anonymous>)
at fulfilled (D:\Learning\nest\project\node_modules\@nestjs\core\injector\injector.js:4:58)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
のほかに、私はいくつかのリコを聞きたいjsonWebToken
変数にタイプany
を使用しないことに関するmmendations。
ソースコードのコメントによれば、「コンポーネントはコンストラクタを介して依存関係を注入できます。これらの依存関係は同じモジュールに属している必要があります。」質問は同じですが、どうすれば外付け部品を注入できますか? –