简体   繁体   English

错误:NullInjectorError:没有路由器提供程序

[英]error:NullInjectorError: No provider for Router

I have to test one component and get this error NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]: NullInjectorError: No provider for Router!**" The component I'm testing have two dependencies and I don't know hot to provide both them in the test with testbed gallery.component.ts我必须测试一个组件并收到此错误 NullInjectorError: R3InjectorError(DynamicTestModule)[AuthenticationService -> Router -> Router]: NullInjectorError: No provider for Router!**" 我正在测试的组件有两个依赖项,但我没有知道 hot 在测试中使用 testbed gallery.component.ts提供它们

constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){
        this.iterableDiffer = iterableDiffers.find([]).create(null);
    }

gallery.component.spec.ts画廊.component.spec.ts

describe('GalleryComponent', () => {

    let component: GalleryComponent;
    let fixture: ComponentFixture<GalleryComponent>
    let authServiceMock: AuthenticationService
    let iterableDifferMock: IterableDiffers

    beforeEach(() => {
        TestBed.configureTestingModule({
        providers:[GalleryComponent, {provide:AuthenticationService}, {provide:IterableDiffers}]
    })
        

        fixture = TestBed.createComponent(GalleryComponent);
        component = fixture.componentInstance;

        authServiceMock = TestBed.inject(AuthenticationService)
    })
...

how to provide both dependencies?如何提供这两个依赖项?
I read the Angular DOC and I didn't find the solution, I sew other asks on SO but without find solutions.我阅读了 Angular DOC,但没有找到解决方案,我在 SO 上提出了其他问题,但没有找到解决方案。
Thanks谢谢

Just import RouterTestingModule to your imports array in the TestBed只需将RouterTestingModule导入RouterTestingModule中的导入数组

TestBed.configureTestingModule({
  imports: [RouterTestingModule],
  providers:[
    GalleryComponent, 
    {provide:AuthenticationService}, 
    {provide:IterableDiffers}
  ]
})

You just inject every dependency you have in the component's constructor:您只需注入组件构造函数中的所有依赖项:

constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers){}

If you need to use Angular router too:如果您也需要使用 Angular 路由器:

constructor( private authService:AuthenticationService, private iterableDiffers: IterableDiffers, private router: Router){}

And no need to add:并且无需添加:

this.iterableDiffer = iterableDiffers.find([]).create(null);

Afterwards, you can just call and work with the dependency you want.之后,您可以调用并使用您想要的依赖项。

暂无
暂无

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

相关问题 Angular 6 错误“NullInjectorError:没有路由器提供程序!” - Angular 6 Error "NullInjectorError: No provider for Router!" NullInjectorError:没有路由器的提供程序! 误差角2 - NullInjectorError: No provider for Router! error angular 2 错误:StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -&gt; Router]:(NullInjectorError:没有路由器的提供者!) - Error: StaticInjectorError(DynamicTestModule)[RouterLinkWithHref -> Router]: (NullInjectorError: No provider for Router!) 错误:NullInjectorError:没有FlashMessagesService的提供程序 - Error: NullInjectorError: No provider for FlashMessagesService 错误 NullInjectorError:没有 HttpClient 的提供者: - ERROR NullInjectorError: No provider for HttpClient: NullInjectorError: StaticInjectorError(ExampleModule)[RouterLinkWithHref -> Router]: NullInjectorError: 没有路由器的提供者 - NullInjectorError: StaticInjectorError(ExampleModule)[RouterLinkWithHref -> Router]: NullInjectorError: No provider for Router 如何使用角度模块? (NullInjectorError:没有路由器的提供程序) - How to use angular modules? (NullInjectorError: No provider for Router) Storybook 错误:NullInjectorError:VgimAdobeAnalyticsService 没有提供程序 - Storybook Error: NullInjectorError: No provider for VgimAdobeAnalyticsService “NullInjectorError:没有 t 的提供者!” @ngrx 出错 - 'NullInjectorError: No provider for t!' error with @ngrx Angular 2错误NullInjectorError:没有Http的提供程序 - Angular 2 error NullInjectorError: No provider for Http
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM