如何将服务注入nestjs-i18n
I18nResolver
文档声明:
要实现自己的解析器(或自定义逻辑),请使用I18nResolver接口。解析器是通过nestjs依赖注入提供的,这样您就可以在需要时注入您自己的服务。
我的救世主看起来如下:
// AppLanguageResolver.ts
import {ExecutionContext, Inject, Injectable, Scope} from "@nestjs/common";
import {I18nResolver} from "nestjs-i18n";
import {UserService} from "@root/services/user-service/user.service";
@Injectable()
export class AppUserLanguageResolver implements I18nResolver {
constructor(private userService: UserService) {}
resolve(context: ExecutionContext): Promise<string | string[] | undefined> | string | string[] | undefined {
console.log("AppUserLanguageResolver");
return undefined;
}
}
在我的app.module.ts
中,ive添加了模块配置:
// app.module.ts
@Module({
providers: [
UserService,
AppUserLanguageResolver,
],
imports: [
I18nModule.forRoot({
fallbackLanguage: 'en',
parser: I18nJsonParser,
parserOptions: {
watch: APP_ENV === AppEnv.Dev,
path: path.join(__dirname, '/i18n/translations'),
},
resolvers: [
AppUserLanguageResolver
]
}),
]
});
但运行该应用程序告诉我,nesjs无法解决依赖关系:
[Nest] 5876 - 20/02/2022, 19:21:58 [ExceptionHandler] Nest can't resolve dependencies of the AppUserLanguageResolver (?). Please make sure that the argument UserService at index [0] is available in the I18nModule context.
Potential solutions:
- If UserService is a provider, is it part of the current I18nModule?
- If UserService is exported from a separate @Module, is that module imported within I18nModule?
@Module({
imports: [ /* the Module containing UserService */ ]
})
+17ms
Error: Nest can't resolve dependencies of the AppUserLanguageResolver (?). Please make sure that the argument UserService at index [0] is available in the I18nModule context.
Potential solutions:
- If UserService is a provider, is it part of the current I18nModule?
- If UserService is exported from a separate @Module, is that module imported within I18nModule?
@Module({
imports: [ /* the Module containing UserService */ ]
})
at Injector.lookupComponentInParentModules (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\injector.js:192:19)
at async Injector.resolveComponentInstance (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\injector.js:148:33)
at async resolveParam (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\injector.js:102:38)
at async Promise.all (index 0)
at async Injector.resolveConstructorParams (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\injector.js:117:27)
at async Injector.loadInstance (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\injector.js:81:9)
at async Injector.loadProvider (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\injector.js:38:9)
at async Promise.all (index 15)
at async InstanceLoader.createInstancesOfProviders (D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\instance-loader.js:43:9)
at async D:\dev\projects\project\workspace\packages\app-backend\node_modules\@nestjs\core\injector\instance-loader.js:28:13
UserService
被注入到Scope.Request
中,但是移除或更改作用域没有任何效果。我也没有发现有关这一问题的任何资料。
我怎么才能解决这个问题?
发布于 2022-02-20 18:49:40
也许你忘了出口UserService了。
发布于 2022-06-26 16:49:01
作为参数传递给nestjs-i18n自定义解析器的构造函数的服务必须是全局的,才能访问。因此,在您的情况下,您应该使您的用户模块是全局的,然后一切都将按预期工作。
例如,在用户模块文件中,您应该执行如下操作:
//user.module.ts
import { Global, Module } from '@nestjs/common';
@Global() // <----- Add this
@Module( {
imports: [ TypeOrmModule.forFeature( [ User ] ) ],
controllers: [ UserController ],
providers: [ UserService ]
} )
export class UserModule { }
https://stackoverflow.com/questions/71197454
复制相似问题