Angular Router常用方法


一、router.navigate的使用

navigate是Router類的一個方法,主要用來跳轉路由。

函數定義:

1
navigate(commands: any[], extras?: NavigationExtras) : Promise`<boolean>`
1
2
3
4
5
6
7
8
9
interface NavigationExtras {
  relativeTo : ActivatedRoute
  queryParams : Params
  fragment : string
  preserveQueryParams : boolean
  preserveFragment : boolean
  skipLocationChange : boolean
  replaceUrl : boolean
}

1.this.router.navigate(['user', 1]);

以根路由為起點跳轉

2.this.router.navigate(['user', 1],{relativeTo: route});

默認值為根路由,設置后相對當前路由跳轉,route是ActivatedRoute的實例,使用需要導入ActivatedRoute

3.this.router.navigate(['user', 1],{ queryParams: { id: 1 } });
路由中傳參數 /user/1?id=1

4.this.router.navigate(['view', 1], { preserveQueryParams: true });

默認值為false,設為true,保留之前路由中的查詢參數/user?id=1 to /view?id=1

5.this.router.navigate(['user', 1],{ fragment: 'top' });

路由中錨點跳轉 /user/1#top

6.this.router.navigate(['/view'], { preserveFragment: true });

默認值為false,設為true,保留之前路由中的錨點/user/1#top to /view#top

7.this.router.navigate(['/user',1], { skipLocationChange: true });

默認值為false,設為true路由跳轉時瀏覽器中的url會保持不變,但是傳入的參數依然有效

8.this.router.navigate(['/user',1], { replaceUrl: true });

未設置時默認為true,設置為false路由不會進行跳轉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM