我們經常用路由傳遞參數,路由主要有三種方式:
第一種:在查詢參數中傳遞數據
{path:"address/:id"} => address/1 => ActivatedRoute.param[id]
在路由中傳遞
<a [routerLink] = "['/address',1]"></a>
點擊事件傳遞
this.router.navigate([‘/address’,2])
//在不同等級頁面跳轉可以用snapshot(快照方式)
this.id = this.routeInfo.snapshot.params["id"]
//相同組件跳轉需要使用subscribe(訂閱方式)
this.routeInfo.params.subscribe((params: Params) => this.id = params["id"] )
第二種:在路由路徑中傳遞參數數據
<a [routerLink] = "['/address']" queryParams= "{id:1}"></a>
this.id = this.routeInfo.snapshot.queryParams["id"]//拿到路由中的參數
第三種:在路由配置中傳遞數據
{path:'home', component: HomeComponent,data:[{isPush:true}] } => ActivatedRoute.data[0][isPush]
//同樣也是有snapshot和subscribe兩種類型
this.isPush = this.routeInfo.snapshot.data[0]["isPush"]
如果有用請支持,謝謝!
