虚位以待(AD)
虚位以待(AD)
首页 > 网页特效 > JavaScript > 详解Angular5 路由传参的3种方法

详解Angular5 路由传参的3种方法
类别:JavaScript   作者:码皇   来源:互联网   点击:

这篇文章主要介绍了详解Angular5 路由传参的3种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文介绍了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 }

参考http://www.jb51.net/article/139125.htm

3.js里的路径跳转

例如:path:/product,component:ProductComponent,data:[{madeInChina:true}]}

获取参数的方式: ActivatedRoute.snapshot.data[0][madeInChina]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

  • angular.js 路由及页面传参示例
相关热词搜索: Angular5 路由传参 Angular 路由传参