2017-12-11 7 views
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。

+0

ソースコードのコメントによれば、「コンポーネントはコンストラクタを介して依存関係を注入できます。これらの依存関係は同じモジュールに属している必要があります。」質問は同じですが、どうすれば外付け部品を注入できますか? –

答えて

0

詳細は悪魔です。そのようなAuthModuleにすることができます "インポート" 他のモジュール:

@Module({ 
    modules: [LibrariesModule], // <= added this line 
    components: [AuthService, JwtStrategy], 
    controllers: [], 
}) 
export class AuthModule { 

} 

出典:here

2番目の質問はまだ開いています。

+0

別に質問をする必要がありますか? –