如何解决 nest.js 中的 DI 问题?
How to resolve DI issue in nest.js?
我想了解如何通过 DI 在 nestjs 中导入 3rd 方库。所以,我有一个 class 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)
此外,我想听听一些关于不在 jsonWebToken
变量中使用类型 any
的建议。
细节决定成败。你可以 "import" 其他模块进入 AuthModule 像这样:
@Module({
modules: [LibrariesModule], // <= added this line
components: [AuthService, JwtStrategy],
controllers: [],
})
export class AuthModule {
}
来源:here
第二题还没开
我想了解如何通过 DI 在 nestjs 中导入 3rd 方库。所以,我有一个 class 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)
此外,我想听听一些关于不在 jsonWebToken
变量中使用类型 any
的建议。
细节决定成败。你可以 "import" 其他模块进入 AuthModule 像这样:
@Module({
modules: [LibrariesModule], // <= added this line
components: [AuthService, JwtStrategy],
controllers: [],
})
export class AuthModule {
}
来源:here
第二题还没开