navigate是Router類的一個方法砌梆,主要用來跳轉(zhuǎn)路由逸月。
函數(shù)定義:
navigate(commands: any[], extras?: NavigationExtras) : Promise <boolean>
interface NavigationExtras {
relativeTo : ActivatedRoute
queryParams : Params
fragment : string
preserveQueryParams : boolean
preserveFragment : boolean
skipLocationChange : boolean
replaceUrl : boolean
}
1.this.router.navigate(['user', 1]);
以根路由為起點跳轉(zhuǎn)
2.this.router.navigate(['user', 1],{relativeTo: route});
默認值為根路由放典,設(shè)置后相對當前路由跳轉(zhuǎn)暑认,route是ActivatedRoute的實例菠齿,使用需要導(dǎo)入ActivatedRoute
3.this.router.navigate(['user', 1],{ queryParams: { id: 1 } });
路由中傳參數(shù) /user/1?id=1
4.this.router.navigate(['view', 1], { preserveQueryParams: true });
默認值為false,設(shè)為true砰蠢,保留之前路由中的查詢參數(shù)/user?id=1 to /view?id=1
5.this.router.navigate(['user', 1],{ fragment: 'top' });
路由中錨點跳轉(zhuǎn) /user/1#top
6.this.router.navigate(['/view'], { preserveFragment: true });
默認值為false磅网,設(shè)為true,保留之前路由中的錨點/user/1#top to /view#top
7.this.router.navigate(['/user',1], { skipLocationChange: true });
默認值為false竹捉,設(shè)為true路由跳轉(zhuǎn)時瀏覽器中的url會保持不變芜辕,但是傳入的參數(shù)依然有效
8.this.router.navigate(['/user',1], { replaceUrl: true });
未設(shè)置時默認為true,設(shè)置為false路由不會進行跳轉(zhuǎn)