Angular: no way to pass an object through a router?

前端 未结 5 2008
走了就别回头了
走了就别回头了 2021-01-26 09:17

I\'ve been trying to find a simple solution.

I have a for loop displaying a list of items. clicking on one of the items routes you to the detailed component and I just wa

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-26 09:37

    From Angular 7.2 you can pass argument to router, see this great link

    In .html you must use [state], e.g.

    Two get the state you has two options, from a component not main-app, see this answer, from the main-app you can subscribe to router.events

    this.router.events.pipe(
      filter(e => e instanceof NavigationStart),
      map(() => this.router.getCurrentNavigation().extras.state)
    ).subscribe((res:any)=>{
       console.log(res)
    })
    

    NOTE: if use this aproach, remember that if you access directly to your "item-details.component", you has any "item"

提交回复
热议问题