Angular中navigate的各种使用情况

发布时间:2022-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Angular中navigate的各种使用情况脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

navigate是Router类的一个方法,主要用来跳转路由。 

函数定义:

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']); 以根路由为起点跳转

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路由不会进行跳转

 

脚本宝典总结

以上是脚本宝典为你收集整理的Angular中navigate的各种使用情况全部内容,希望文章能够帮你解决Angular中navigate的各种使用情况所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。