Subscribe to both route params and queryParams in Angular 2

前端 未结 7 1917
粉色の甜心
粉色の甜心 2020-12-14 00:45

I have set up the following routing system

export const MyRoutes: Routes = [
  {path: \'\', redirectTo         


        
7条回答
  •  孤城傲影
    2020-12-14 01:21

    The proposed solutions using combineLatest do not work properly. After the first route change, you will get an event every time the param or queryParam value changes, which means you get calls in an intermediate state when one has changed but the other has not, very bad.

    @rekna's solution pretty much works for me, except I wasn't getting an event when my app was first loaded, so here's my modification to @rekna's solution:

    this.routeUpdate(this.activatedRoute.snapshot);
    this.routeSubscription = this.router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .subscribe(event => this.routeUpdate(this.activatedRoute.snapshot));
    

    Where routeUpdate is a method to do whatever parameter handling is required.

提交回复
热议问题