ng4 路由多参数传参以及接收

来源:互联网 发布:东方娱乐有什么软件 编辑:程序博客网 时间:2024/06/14 23:36

最近写ng4项目碰到要传多个参数,当然,这肯定是会碰到的。但是奇怪的是,我在百度上居然没怎么查到传多个参数以及接收多个参数的例子。每个看去基本都是官网上demo单个参数的使用。

不废话了,我自己来记录下。

1.导航需要跳转的地址,并以json对象形式传入多个参数,key可以随便取,但后面页面接收参数获取
的值一定要跟这个key对应才行。

import { Router } from '@angular/router';//需要引入的库类。 constructor( //在构造器中声明        private router:Router,    ) { } /**     * 导航到应用详情     */    goApplicationDetail(instanceId:number,ownerShip:boolean){        return this.router.navigate(['/console/details/appDetail',{"instanceId":instanceId,"ownerShip":ownerShip}]);    }

2.接收的路由地址中不需要定义接收任何参数

 { path: "appDetail", component:  DetailsComponent}

3.在跳转后的Component组件中,我是在构造器中获取。

import {ActivatedRoute } from '@angular/router';//需要引入的库类 constructor(                private route:ActivatedRoute               ) {        this.appId = this.route.params["value"].instanceId;        this.tempOwnerShip = this.route.params["value"].ownerShip;        this.tempOwnerShip==="true"?this.ownerShip =true:this.ownerShip =false;        console.log("appid="+this.appId);        console.log("ownerShip="+this.ownerShip);    }

不知道大家有没注意到一点,就是不管参数是任何类型(boolean,number….)传过来通通都会变为string类型。所以如果有判断的话要记得再转换下。我这里用三目运算判断转换。

原创粉丝点击