简体   繁体   English

升级到 Angular 12 - NullInjectorError: No provider for InjectionToken

[英]Upgrade to Angular 12 - NullInjectorError: No provider for InjectionToken

Original implementation was based on the pattern described in最初的实现是基于描述的模式

https://shashankvivek-7.medium.com/forroot-injectiontoken-in-action-angular-c2bab9959207 https://shashankvivek-7.medium.com/forroot-injectiontoken-in-action-angular-c2bab9959207

It was working until I upgraded it to angular 12. After the upgrade I kept getting the NullInjector error.在我将其升级到 angular 12 之前,它一直在工作。升级后,我不断收到 NullInjector 错误。 Unable to resolve it anyway, I ended up creating two vanilla simple projects for Angular 11 and 12 each.无论如何都无法解决它,我最终为 Angular 11 和 12 创建了两个普通的简单项目。 Additionally, I moved the token provider from the project module to the appmodule.此外,我将令牌提供程序从项目模块移至应用模块。

Angular 12 Sample https://drive.google.com/file/d/1lE8RL6s8L9n6zQYqVOaLqMW9ikMfMgSu/view?usp=sharing Angular 12 样品https://drive.google.com/file/d/1lE8RL6s8L9n6zQYqVOaLqMW9ikMfMgSu/view?usp=sharing

Angular 11 Sample https://drive.google.com/file/d/1HxhCz3i5nmrK8Zj0eddTlk8zz-29lwlh/view?usp=sharing Angular 11 样品https://drive.google.com/file/d/1HxhCz3i5nmrK8Zj0eddTlk8zz-29lwlh/view?usp=sharing

The angular 11 version works (even prod mode), but the other version runs into the same error. angular 11 版本有效(即使是 prod 模式),但其他版本遇到相同的错误。

喷油器错误

This is what it looks like inside the debugger这是调试器内部的样子

调试器

It looks like the line它看起来像线

let record = this.records.get(token);

in R3Injector.get always fails to find the record by the token, even though the record exists in the list of records, as can be seen in the image (index 58).在 R3Injector.get 中始终无法通过令牌找到记录,即使该记录存在于记录列表中,如图所示(索引 58)。 It is because of this that a duplicate record at index 78, gets added.正因为如此,才会添加索引 78 处的重复记录。 The key and the token parameter seem to be identical.密钥和令牌参数似乎相同。

I'm unable to find any definitive information about it, or a way to fix it.我找不到任何关于它的确切信息或修复它的方法。

Learned the hard way the perils of a bad import like the following (auto inserted by IDEs like VS Code)艰难地了解了如下错误导入的危险(由 VS Code 等 IDE 自动插入)

import { Xyz } from 'projects/shared/src/public-api';

One such mis-imported object led to doubly defined SharedConfigToken in the generated main.js.其中一个错误导入的 object 导致在生成的 main.js 中双重定义 SharedConfigToken。 This meant the injector would never find the record because it was inserted with one SharedConfigToken but that got overwritten later due to the double import.这意味着注入器永远找不到记录,因为它是用一个 SharedConfigToken 插入的,但后来由于双重导入而被覆盖。 Since Map key comparison is SameValueZero it could never lookup the token, hence the error.由于 Map 键比较是 SameValueZero 它永远无法查找令牌,因此出现错误。

It wasted a lot of time from my life.它浪费了我生命中的很多时间。 I hope the finding helps someone.我希望这一发现对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 5 升级 NullInjectorError:没有 InjectionToken LocaleId 的提供者 - Angular 5 Upgrade NullInjectorError: No provider for InjectionToken LocaleId Angular 5和lerna NullInjectorError:没有InjectionToken的提供者 - Angular 5 and lerna NullInjectorError: No provider for InjectionToken NullInjectorError:Angular 2 中没有 InjectionToken LOCAL_STORAGE 的提供者 - NullInjectorError: No provider for InjectionToken LOCAL_STORAGE in Angular 2 angular 业力 - NullInjectorError:没有 InjectionToken 配置的提供者 - angular karma - NullInjectorError: No provider for InjectionToken config Quill Angular 错误:NullInjectorError: No provider for InjectionToken config - Quill Angular Error: NullInjectorError: No provider for InjectionToken config NullInjectorError:没有 InjectionToken _ViewRepeater 的提供者 - NullInjectorError: No provider for InjectionToken _ViewRepeater NullInjectorError:没有 InjectionToken MatDialogData 的提供者 - NullInjectorError: No provider for InjectionToken MatDialogData NullInjectorError:没有InjectionToken DocumentToken的提供者 - NullInjectorError: No provider for InjectionToken DocumentToken NullInjectorError:没有 InjectionToken ToastConfig 的提供者! 在 Angular + Electron 项目的 jasmine 规范中 - NullInjectorError: No provider for InjectionToken ToastConfig! in jasmine spec for Angular + Electron project MSAL Angular - NullInjectorError:没有提供 InjectionToken MSAL_CONFIG - MSAL Angular - NullInjectorError: No provider for InjectionToken MSAL_CONFIG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM