本文介紹了Angular5 路由傳參,一共3種方法。分享給大家,具體如下:
1.問號后面帶的參數,獲取參數的方式:ActivatedRoute.queryParams[id]
例如:/product?id=1&name=iphone還可以是: [ routerLink]= "['/books']" [ queryParams]= "{bookname:'《活著》'}
代碼:html
<h4>Messages</h4><p>Total:{{msgs.total}}</p><div *ngFor="let item of msgs.data"> <b>{{item.name}}</b>: <a [routerLink]="['/next',item.id]" [queryParams]="{id:item.id, msg:item.msg, name:item.name}">{{item.msg}}</a> <a routerLink="/final">Reply</a><p></p></div>
獲取參數js
ngOnInit() { let obj = this.route.queryParams["_value"]; console.log(obj);}
2.冒號形式,
例如:path:/product/:id
獲取參數的方式:ActivatedRoute.params[id]
上邊html代碼中第一個routelink就是。
另外需配置路由
{ path:'listDetail/:id', component:ListDetailComponent }
參考//www.49028c.com/article/139125.htm
3.js里的路徑跳轉
例如:path:/product,component:ProductComponent,data:[{madeInChina:true}]}
獲取參數的方式: ActivatedRoute.snapshot.data[0][madeInChina]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答