Angular升级至12后useFactory配置的LOCALE_ID始终为undefined如何解决
Angular 8.2升级至12后LOCALE_ID通过useFactory注入返回undefined问题
问题描述
将Angular版本从8.2逐次升级至12后,原有功能出现异常:在app.module.ts中定义的LOCALE_ID提供商始终返回undefined,替换为静态赋值后功能恢复正常。
复现代码
原代码(Angular8.2正常运行,升级后失效)
{ provide: LOCALE_ID, useFactory: (translate: TranslateService) => translate.currentLang, deps: [TranslateService] }
临时验证代码(功能可恢复正常)
{ provide: LOCALE_ID, useValue: 'en' }
附加参考信息
- 使用的ngx-translate版本为13.0.0
- 服务与管道中均通过以下方式注入LOCALE_ID:
constructor(@Inject(LOCALE_ID) private locale: string)
- AppModule类中TranslateService为undefined,但应用加载完成后,其余位置可正常访问该服务。
最终解决方案
将该LOCALE_ID提供商从app.module.ts迁移到app.component.ts的提供商列表中即可修复该问题。
内容的提问来源于stack exchange,提问作者ArtoriasDF
相关产品推荐
相关产品推荐

