日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 几个经常用到的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

總結

以上是生活随笔為你收集整理的几个经常用到的angular路由Router、ActivatedRoute 知识点:嵌套路由、路由跳转、路由传参、路由参数获取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。