2017-12-11 10 views
1

DI를 통해 nestjs에서 타사 라이브러리를 가져 오는 방법을 알고 싶습니다.nest.js에서 DI 문제를 해결하는 방법은 무엇입니까?

export class AuthService { 
    constructor(
    @Inject(constants.JWT) private jsonWebToken: any, 
    ){} 
    .... 
} 

JWT 제공 : 그래서, 클래스 AuthService

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

두 번째 질문은 여전히 ​​열립니다.

+0

별도의 질문을하거나 계속 작성해야합니까? –