2 routes for one Angular component

I am wondering if it is possible to have 2 different routes that point to one separate component? For instance:

{ path: 'signin', component: SignInComponent },
{ path: 'tenant/:id/signin', component: SignInComponent }

I ask because I have some kind of wired behavior.

+4
source share
1 answer

Yes, you can use 2 different routes that point to the same component. But when you move from a route to another route that uses the same component as the previous route, angular reuses the instance of the component. It does not create a new instance of your component.

"signin" "/: id/signin", angular SignInComponent, "signin". init SignInComponent . init. params ActivatedRoute . - , :

- ActivatedRoute

ngOnInit() {
  this.route.params
    .switchMap((params: Params) => this.service.getHero(+params['id']))
    .subscribe((hero: Hero) => this.hero = hero);
}

switchMap Observable Observable. , .   - . , , .

+4

Source: https://habr.com/ru/post/1664515/


All Articles