angular2中router路由跳转navigate的使用与刷新页面问题详解

本文主要介绍的是angular2中router路由跳转navigate的使用与刷新页面问题的相关内容分享出供大家参考学习,下面来看看详细的介绍:

一、router.navigate的使用

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

函数定义:

rush:js;"> navigate(commands: any[],extras?: NavigationExtras) : Promise``
rush:js;"> 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',{ queryParams: { id: 1 } }); 路由中传参数 /user/1?id=1

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

认值为false,设为true,保留之前路由中的查询参数/user?id=1 to /view?id=1

5.this.router.navigate(['user',{ 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',{ skipLocationChange: true });

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

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

未设置时认为true,设置为false路由不会进行跳转

二、router.navigate刷新页面问题

造成这个问题一般是因为我们在

表单中使用
rush:js;"> toDetail() { this._router.navigate(['/detail']); }

解决方法

1.添加type

rush:xhtml;">

2.click添加false

rush:xhtml;">

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用Angular.js能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程之家的支持

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《》、《》及《

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...