How to mock route.snapshot.params?

后端 未结 2 1737
执念已碎
执念已碎 2021-02-02 09:21

In my Angular 4 component I have something like:

constructor(private route: ActivatedRoute) {
}

ngOnInit() {
  this.myId          


        
相关标签:
2条回答
  • 2021-02-02 09:55

    If using route.snapshot.paramMap.get( 'uuid' ) instead:

    import { ActivatedRoute, convertToParamMap } from '@angular/router';
    
    {
        provide: ActivatedRoute, useValue:
            { snapshot: { paramMap: convertToParamMap( { 'uuid': '99-88-77' } ) } }
    }
    
    0 讨论(0)
  • 2021-02-02 10:16

    Ok, I've found how to mock ActivatedRoute snapshot in the simple way. Something like this works for me:

    providers: [MyComponent, {
      provide: ActivatedRoute,
      useValue: {snapshot: {params: {'myId': '123'}}}
    }
    

    Thanks :)

    0 讨论(0)
提交回复
热议问题