Subscribe to both route params and queryParams in Angular 2

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

I have set up the following routing system

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


        
7条回答
  •  渐次进展
    2020-12-14 01:37

    An alternative (on Angular 7+) is to subscribe to the ActivatedRoute url observable and use "withLatestFrom" to get the latest paramsMap and queryParamsMap. It appears that the params are set before the url observable emits:

    this.route.url.pipe(
      withLatestFrom(this.route.paramMap, this.route.queryParamMap)
    ).subscribe(([url, paramMap, queryParamMap]) => {
      // Do something with url, paramsMap and queryParamsMap
    });
    

    https://rxjs-dev.firebaseapp.com/api/operators/withLatestFrom

    https://angular.io/api/router/ActivatedRoute

提交回复
热议问题