[英]Angular: Karma giving error Error: NullInjectorError: No provider for ElementRef! after upgrade from v7 to v8
Very similar to my last Karma upgrade issue in this post , I have all my unit test for a component failing after upgrading from v7 to v8.与我在这篇文章中的上一个 Karma 升级问题非常相似,从 v7 升级到 v8 后,我对组件的所有单元测试都失败了。 Once again due to a custom component from a shared library.再次由于来自共享库的自定义组件。
So here I get the following..所以在这里我得到以下..
NullInjectorError: StaticInjectorError(DynamicTestModule)[ProgressIndicatorComponent -> ElementRef]:
StaticInjectorError(Platform: core)[ProgressIndicatorComponent -> ElementRef]:
NullInjectorError: No provider for ElementRef!
Error: NullInjectorError: No provider for ElementRef!
at NullInjector.get (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:725:1) [angular]
at resolveToken (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11918:1) [angular]
at tryResolveToken (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11862:1) [angular]
at StaticInjector.get (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11764:1) [angular]
at resolveToken (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11918:1) [angular]
at tryResolveToken (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11862:1) [angular]
at StaticInjector.get (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:11764:1) [angular]
at resolveNgModuleDep (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:20234:1) [angular]
at NgModuleRef_.get (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:20905:1) [angular]
at resolveDep (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:21276:1) [angular]
at createClass (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:21152:1) [angular]
at createDirectiveInstance (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:21027:1) [angular]
at createViewNodes (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:29387:1) [angular]
at callViewAction (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm5/core.js:29703:1) [angular]
but this time I do have the ProgressIndicatorComponent
(the problem component here) in my declarations.但这次我的声明中确实有ProgressIndicatorComponent
(这里的问题组件)。
The component has the following constructor.该组件具有以下构造函数。
constructor(
private elRef: ElementRef,
private renderer: Renderer2,
private changeDetector: ChangeDetectorRef) { }
If I import the module containing this component instead (and remove it from the declarations) I get如果我导入包含此组件的模块(并将其从声明中删除),我会得到
Failed: Template parse errors:
Can't bind to 'visible' since it isn't a known property of 'mm-progress-indicator'.
1. If 'my-progress-indicator' is an Angular component and it has 'visible' input, then verify that it is part of this module.
If I include both, ie import the module AND declare the component, I get..如果我同时包含两者,即导入模块并声明组件,我得到..
NullInjectorError: StaticInjectorError(DynamicTestModule)[ProgressIndicatorComponent -> ElementRef]:
StaticInjectorError(Platform: core)[ProgressIndicatorComponent -> ElementRef]:
NullInjectorError: No provider for ElementRef!
Now out of ideas (the missing declaration worked for me last time)现在没有想法(上次缺少的声明对我有用)
Anyone any ideas?有人有什么想法吗?
This time, adding the following to tsconfig.json
seemed to fix the issue这次,将以下内容添加到tsconfig.json
似乎可以解决问题
"paths": {
"@angular/*": [
"./node_modules/@angular/*"
],
My global angular cli is actually still at v7, perhaps it was trying to use this?我的全局 angular cli 实际上仍处于 v7,也许它正在尝试使用它? Also not sure why only the unit tests needed this, where as the project would build and run fine.也不知道为什么只有单元测试需要这个,因为项目可以构建和运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.