几个经常用到的angular路由Router、ActivatedRoute 知识点:嵌套路由、路由跳转、路由传参、路由参数获取

深度玩家可移步Angular - 常见路由任务

1、嵌套路由

const routes: Routes = [
  {
    path: 'first',
    component: FirstComponent,//同步加载

    //这就是嵌套路由↓
    children:[
      {path:'first-sub1',component:firstSub1Component},
      {path:'first-sub2',component:firstSub2Component},
    ]

  },
];

深度玩家可移步Angular - Router 

2、路由跳转

<!--在html标签上加跳转-->
<a routerLink="../second-component">Relative Route to second component</a>
import { Router } from '@angular/router';
 
constructor(private router: Router) { }

//各种跳转方式
this.router.navigate(['items'], { relativeTo: this.route });
this.router.navigateByUrl("/team/33/user/11");
this.router.navigateByUrl("/team/33/user/11", { skipLocationChange: true });
this.router.navigate(['team', 33, 'user', 11], {relativeTo: route});
this.router.navigate(['team', 33, 'user', 11], {relativeTo: route, skipLocationChange: true});

3、路由传参

//1.以根路由跳转/login
this.router.navigate(['login']);

//2.设置relativeTo相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute
this.router.navigate(['login', 1],{relativeTo: route}); 

//3.路由中传参数 /login?name=1
this.router.navigate(['login', 1],{ queryParams: { name: 1 } }); 

//4.preserveQueryParams默认值为false,设为true,保留之前路由中的查询参数/login?name=1 to /home?name=1
this.router.navigate(['home'], { preserveQueryParams: true }); 

//5.路由中锚点跳转 /home#top
this.router.navigate(['home'],{ fragment: 'top' });

//6.preserveFragment默认为false,设为true,保留之前路由中的锚点/home#top to /role#top
this.router.navigate(['/role'], { preserveFragment: true }); 

//7.skipLocationChange默认为false,设为true,路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效
this.router.navigate(['/home'], { skipLocationChange: true });

//8.replaceUrl默认为true,设为false,路由不会进行跳转
this.router.navigate(['/home'], { replaceUrl: true }); 

4、路由参数获取

import { ActivatedRoute } from '@angular/router';

constructor( private route: ActivatedRoute ) {}

//第一种:在查询参数中传递数据----------------------------------------
//在路由中传递
{path:"address/:id"} => address/1 => ActivatedRoute.param.id

//点击事件传递
<a [routerLink] = "['/address',1]">

//在不同等级页面跳转可以用snapshot(快照方式)
this.route.snapshot.params.id
this.route.snapshot.queryParams.id

//相同组件跳转需要使用subscribe(订阅方式)
this.route.params.subscribe((params: Params) => this.id = params.id )


//第二种:在路由路径中传递参数数据----------------------------------------
<a [routerLink] = "['/address']" queryParams= "{id:1}">
this.route.snapshot.queryParams.id//拿到路由中的参数(即浏览器网页地址中url后面的?参数=)


//第三种:在路由配置中传递数据----------------------------------------
{path:'home', component: HomeComponent,data:[{isPush:true}] } 
=> 
ActivatedRoute.data[0][isPush]
//同样也是有snapshot和subscribe两种类型
this.route.snapshot.data[0]['isPush']

如何修改当当前网页中url的参数Angular如何修改当前页面网页浏览器url后面?param1=xxx¶m2=xxx参数_你挚爱的强哥❤给你发来1条消息❤-CSDN博客app.component.html<button (click)="location.replaceState('/a/a/','?id=1&pageIndex=2&pageSize=10#hashValue');path1=this.location.path(true);path2=this.location.path(false)">修改当前浏览器url参数</button><p>【含hash的url】{{path1}}</p>https://s-z-q.blog.csdn.net/article/details/120576030

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值