angular15 路由传参

一 在模版实现路由跳转功能
1.

<a  routerLink = "./component-a"> 跳转a组件</a >
<a  [routerLink] = "['./component-a']"> 跳转a组件</a >

2.active样式

  <a [routerLink]="['./component-a']"  routerLinkActive = "active"> 跳转a组件</a>

3.传参
1) 通过queryParams

<a routerLink=“./component-a” [queryParams] = “{id:‘1’}”> 跳转a组件
url地址:/component-a?id=1
接收值:

 this.activateRoute.queryParams.subscribe(params=>{});

2)通过activeParams传参数

  <a [routerLink]="['./component-a' ,{id:'2',aaa:'111'}]" routerLinkActive="active">跳转a组件</a>

接收值:

   this.activateRoute.params.subscribe(params=>{});  
   this.activateRoute.snapshot.paramMap.get()

3)动态路由传值
这种写法需要在了路由配置模块 且不能再使用上两种方法

{ path: 'component-a/:id', component: ComponentAComponent },
  <a [routerLink]="['./component-a', 3]" routerLinkActive="active" >跳转a组件</a>

接收值:

this.activateRoute.params.subscribe(params=>{});  this.activateRoute.snapshot.paramMap.get()

二:在ts中实现路由跳转功能
有两个函数实现

  1. navigateByUrl() 基于所提供的 URL 进行导航,必须使用绝对路径。
    两个入参
    url string | UrlTree 一个绝对URL。该函数不会对当前 URL 做任何修改。
    extras NavigationBehaviorOptions 一个包含一组属性的对象,它会修改导航策略。 该函数会忽略 NavigationExtras 中任何可能会改变所提供的 URL 的属性,可选值。默认值为 { skipLocationChange: false }。
    返回值为promise
    可以使用动态路由传值

2.navigate

  this.router.navigate(['component-b']); 以根路由跳转route
    this.router.navigate(['component-b'], { relativeTo: this.activeRoute }); 以当前路由跳转
    this.router.navigate(['/router-study/component-b']);  固定以根路由跳转
    this.router.navigate(['component-b', '3'], {
      queryParams: { aaa: 333, bbb: 3333 },
      relativeTo: this.activeRoute
    });动态传参和查询参数

接收查询参数值: this.activateRoute.queryParams.subscribe(params=>{});
接收动态传参值: this.activateRoute.params.subscribe(params=>{}); 通过this.activateRoute.snapshot.paramMap.get()也能一次性获取传值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular中,可以使用参数传递来向组件传递数据。有几种不同的方式可以实现参数传递: 1. 通过路由传递参数:可以在路由配置中定义参数,并在URL中传递参数值。在组件中,可以使用ActivatedRoute服务来访问传递的参数。例如: 在路由配置中: ``` { path: 'example/:id', component: ExampleComponent } ``` 在组件中: ```typescript import { ActivatedRoute } from '@angular/router'; constructor(private route: ActivatedRoute) { } ngOnInit() { this.route.params.subscribe(params => { const id = params['id']; // 使用传递的参数 }); } ``` 2. 通过属性绑定传递参数:可以在组件模板中使用属性绑定来传递参数。在父组件中,使用属性绑定将数据传递给子组件。在子组件中,通过@Input装饰器接收传递的参数。例如: 在父组件模板中: ```html <app-child [param]="value"></app-child> ``` 在子组件中: ```typescript import { Input } from '@angular/core'; @Input() param: any; // 使用传递的参数 ``` 3. 通过服务传递参数:可以创建一个共享的服务,在多个组件之间共享数据。在提供者中定义一个属性来保存要传递的参数,在需要使用参数的组件中注入该服务并访问参数。例如: 创建一个参数服务: ```typescript import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class ParamService { param: any; } ``` 在组件中使用参数服务: ```typescript import { ParamService } from './param.service'; constructor(private paramService: ParamService) { } ngOnInit() { const param = this.paramService.param; // 使用传递的参数 } ``` 以上是一些常见的Angular中传递参数的方式,你可以根据你的需求选择适合的方式来传递参数。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值