简体   繁体   English

Angular:Karma 给出错误错误:NullInjectorError:没有提供 ElementRef! 从 v7 升级到 v8 后

[英]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.

相关问题 Angular Angular 从 v7 升级到 v8 后出现 Karma 错误 - Angular Karma error after upgrading Angular from v7 to v8 从 Angular v7 升级到 v8 后,显示“my-custom-element”的 Vscode 不是已知元素 - Vscode displaying "my-custom-element" is not a known element after upgrade from Angular v7 to v8 最新版本为V8时,如何将Angular从V6升级到V7? - How to upgrade Angular from V6 to V7 when the latest version is V8? Angular Karma:从 Angular v8 升级到 v12 后,await fixture.whenStable 总是超时 - Angular Karma: await fixture.whenStable always times out after upgrade from Angular v8 to v12 在我的Angular项目上升级到ngrx v8后,如何修复“NullInjectorError:没有ProviderToken @ ngrx /路由器存储配置的提供商!”? - How do I fix “ NullInjectorError: No provider for InjectionToken @ngrx/router-store Configuration!” after upgrading to ngrx v8 on my Angular project? 角度版本升级出现 404 错误(v5 到 v6) - angular version upgrade giving 404 error (v5 to v6) ng upgrade命令无法将cli从v7升级到v8 - ng upgrade command fails for upgrading cli from v7 to v8 将我的Angular应用程序从v7升级到v8 ng后,serve不再能够找到启动它的项目 - After upgrading my Angular app from v7 to v8 ng serve is no longer able to find the project to start it up Angular 使用 angular/cli v8 构建 v7 库 - Angular build v7 library with angular/cli v8 Angular V11:NullInjectorError:没有 ControlContainer 的提供程序 - Angular V11: NullInjectorError: No provider for ControlContainer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM