angular2 router中navigate方法详解

2017-03-21阅读(13054)评论(0)牵着狗狗看MM

苏州实时公交查询

navigate是Router类的一个方法,主要用来跳转路由。
函数定义:

[plain]navigate(commands: any[], extras?: NavigationExtras) : Promise`<boolean>
[/plain]

[plain]interface NavigationExtras {
relativeTo : ActivatedRoute
queryParams : Params
fragment : string
preserveQueryParams : boolean
preserveFragment : boolean
skipLocationChange : boolean
replaceUrl : boolean
}
[/plain]

this.router.navigate(['user', 1]);
以根路由为起点跳转

this.router.navigate(['user', 1],{relativeTo: route});
默认值为根路由,设置后相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

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

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

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

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

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

默认值为false,设为true路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效

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

赞(0)
转载请注明来源:Web前端(W3Cways.com) - Web前端学习之路 » angular2 router中navigate方法详解
分享到: 更多 (0)