Angular中路由跳转并携带参数的三种方式

本文详细介绍了Angular中路由传值的两种方式:通过queryParams和动态路由参数。在静态路由中,使用queryParams传递参数并在组件中订阅查询参数来接收。而在动态路由中,定义路由路径为`:id`,通过`/devicepay/:id`的方式传递参数,同样在目标组件中订阅params来获取。此外,还展示了如何通过JS调用router.navigate进行传值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.路由传值

传递
<a [routerLink]="['/devicepay']" [queryParams]="{id:key}">
接收
import {ActivatedRoute} from '@angular/router'
constructor(public route:ActivatedRoute) { }

this.route.queryParams.subscribe((res)=>{
	console.log(res)
})

二.动态路由传值

动态路由需要定义
{ path: 'devicepay/:id',component:DevicepayComponent},
传递
<a [routerLink]="['/devicepay/',key]">
this.router.navigate([`/devicepay/`${name}])
接收
import {ActivatedRoute} from '@angular/router'
constructor(public route:ActivatedRoute) { }
this.route.params.subscribe((res)=>{
	console.log(res)
})

三.JS跳转

传递
import { Router} from '@angular/router'
constructor( public router:Router) { }

navigate(key):void{
    this.router.navigate(['/devicepay/'],{queryParams:{id:key}})
}
接收
constructor(private route: ActivatedRoute) {
   console.log(this.route.queryParams);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值