![](/img/trans.png)
[英]Angular Karma error after upgrading Angular from v7 to v8
[英]Angular: Karma giving error Error: NullInjectorError: No provider for ElementRef! after upgrade from v7 to v8
與我在這篇文章中的上一個 Karma 升級問題非常相似,從 v7 升級到 v8 后,我對組件的所有單元測試都失敗了。 再次由於來自共享庫的自定義組件。
所以在這里我得到以下..
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]
但這次我的聲明中確實有ProgressIndicatorComponent
(這里的問題組件)。
該組件具有以下構造函數。
constructor(
private elRef: ElementRef,
private renderer: Renderer2,
private changeDetector: ChangeDetectorRef) { }
如果我導入包含此組件的模塊(並將其從聲明中刪除),我會得到
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.
如果我同時包含兩者,即導入模塊並聲明組件,我得到..
NullInjectorError: StaticInjectorError(DynamicTestModule)[ProgressIndicatorComponent -> ElementRef]:
StaticInjectorError(Platform: core)[ProgressIndicatorComponent -> ElementRef]:
NullInjectorError: No provider for ElementRef!
現在沒有想法(上次缺少的聲明對我有用)
有人有什么想法嗎?
這次,將以下內容添加到tsconfig.json
似乎可以解決問題
"paths": {
"@angular/*": [
"./node_modules/@angular/*"
],
我的全局 angular cli 實際上仍處於 v7,也許它正在嘗試使用它? 也不知道為什么只有單元測試需要這個,因為項目可以構建和運行良好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.