本文介紹了Angular5 路由傳參,一共3種方法。分享給大家,具體如下:
1.問(wèn)號(hào)后面帶的參數(shù),獲取參數(shù)的方式: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>
獲取參數(shù)js
ngOnInit() { let obj = this.route.queryParams["_value"]; console.log(obj); }
2.冒號(hào)形式,
例如:path:/product/:id
獲取參數(shù)的方式:ActivatedRoute.params[id]
上邊html代碼中第一個(gè)routelink就是。
另外需配置路由
{ path:'listDetail/:id', component:ListDetailComponent }
參考//www.gxlcms.com/article/139125.htm
3.js里的路徑跳轉(zhuǎn)
例如:path:/product,component:ProductComponent,data:[{madeInChina:true}]}
獲取參數(shù)的方式: ActivatedRoute.snapshot.data[0][madeInChina]
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com