我想了解如何通过DI导入nestjs中的第三方库。所以,我有一个班级AuthService
:
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)
此外,我想听一些关于不在any
变量中使用jsonWebToken
类型的建议。
答案 0 :(得分:1)
魔鬼在细节上。您可以将其他模块“导入”到AuthModule中,如下所示:
@Module({
modules: [LibrariesModule], // <= added this line
components: [AuthService, JwtStrategy],
controllers: [],
})
export class AuthModule {
}
来源:here
第二个问题仍然存在。