这次给大家带来Angular5路由传参使用详解,Angular5路由传参使用的注意事项有哪些,下面就是实战案例,一起来看一下。
1.问号后面带的参数,获取参数的方式:ActivatedRoute.queryParams[id]
例如:/product?id=1&name=iphone还可以是: [ routerLink]= "['/books']" [ queryParams]= "{bookname:'《活着》'}
代码:html
<h4>Messages</h4>
<p>Total:{{msgs.total}}</p>
<p *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>
</p>获取参数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
}参考http://www.jb51.net/article/139125.htm
3.js里的路径跳转
例如:path:/product,component:ProductComponent,data:[{madeInChina:true}]}
获取参数的方式: ActivatedRoute.snapshot.data[0][madeInChina]
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是Angular5路由传参使用详解的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号