Subscribe to both route params and queryParams in Angular 2

前端 未结 7 1881
粉色の甜心
粉色の甜心 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

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