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

二、router.navigate刷新页面问题

造成这个问题一般是因为我们在<form>表单中使用<button>时忘记添加type属性,在表单中,如果忘记给按钮添加属性,会默认为submit

1
<button (click)="toDetail()">detail</button>
1
2
3
toDetail() {
 this._router.navigate(['/detail']);
}

解决方法: 

1.添加type

1
<button type="button" (click)="toDetail()">detail</button>

2.click添加false

1
<button (click)="toDetail();false">detail</button>

总结

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

navigate的更多相关文章

  1. (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

    (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKi ...

  2. window.navigate 与 window.location.href 的使用区别介绍

    window.navigate(sURL)方法是针对IE的,不适用于FF,在HTML DOM Window Object中,根本没有列出window.navigate方法. 要在javascript中 ...

  3. 在wp中,使用NavigationService.Navigate导航页面出现错误

    我们在WP项目中采用页面导航时候,经常会使用以下代码 NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relati ...

  4. angular 的navigate 各种使用情况

    navigate是Router类的一个方法,主要用来跳转路由. 函数定义: navigate(commands: any[], extras?: NavigationExtras) : Promise ...

  5. Angular4中路由Router类的跳转navigate

    最近一直在学习angular4,它确实比以前有了很大的变化和改进,好多地方也不是那么容易就能理解,好在官方的文档和例子是中文,对英文不太好的还是有很大帮助去学习. 官方地址:https://angul ...

  6. ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)

    页面跳转时,报  Undefined is not an Object(evaluating this2.props.navigation.navigate) 出错原因:在一个页面组件中调用了另一个组 ...

  7. 在webBrowser1.Navigate(url)中设置Cookie的注意点

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static exte ...

  8. react-navigation 3.x版本的push、navigate、goback、pop、dispatch等常用方法

    一.方法简介 1. 应用中的每个页面组件都会自动提供 this.props.navigation this.props.navigation可以获取的一些方法: navigate - 转到另一个页面, ...

  9. driver.get()和driver.navigate().to()到底有什么不同?-----Selenium快速入门(四)

    大家都知道,这两个方法都是跳转到指定的url地址,那么这两个方法有什么不同呢?遇到这种情况,第一反应就是查查官方的文档. 官方文档的说法是:Load a new web page in the cur ...

  10. angular2 Router类中的路由跳转navigate

    navigate是Router类的一个方法,主要用来路由跳转. 函数定义 navigate(commands: any[], extras?: NavigationExtras) : Promise` ...

随机推荐

  1. 陈立伟 - MultiCharts快易通(2013年8月2日)

    <MultiCharts快易通> 作 者:陈立伟 译 者: 系 列:寰宇程式交易312--挑战程式交易系列1 出 版:寰宇出版股份有限公司 字 数:千字 阅读完成:2013年8月2日

  2. datatables .fnDraw is not a function

    Datatables中文网:http://dt.thxopen.com/manual/api.html API DataTables 提供的可以操作表格数据的API,有下面六个关键部分: 表格(tab ...

  3. iOS:quartz2D绘图(处理图像,绘制图像并添加水印)

    绘制图像既可以重写drawRect:方法并在该方法中绘制,也可以不用重写该方法,它有封装好的函数获取自己的图像绘制上下文,即UIGraphicsBeginImageContext(CGSize siz ...

  4. 纯CSS实现文字一行居中,多行左对齐的方法

    其实这种需求还是蛮常见的,主要用于产品列表页面,用于产品图片下面,显示产品的名称. 想要实现的效果为: 未知文字长度,当文字长度小于盒子宽度,也就是一行时,文字居中. 当文字长度大于盒子的宽度,会自动 ...

  5. ZOJ3622 Magic Number(水题)

    分析: 举个样例xxx(三位数)为魔力数,则xxx|(xxx+1000*y),那么xxx|1000,这个就是结论 同理:四位数xxxx|10000,五位数xxxxx|100000 代码: #inclu ...

  6. java map 分析

    java 8 对HashMap进行了优化, 当碰撞时使用TreeNode的二分方法查找数据: 但是当数据碰撞厉害的时候, table有很多浪费. table 大小等于size/factor, 当碰撞很 ...

  7. LINUX 下编译不通过解答

    在linux下编译android源码或者webkit等程序源码时,不论在源码下加什么错误,编译器都默认正确,检索不到错误,此时,可能是之前编译的生成文件默认编译器不再检索编译新修改过的文件,只是发现修 ...

  8. 【笔记】git 的常用操作命令(持续更新。。。)

    项目正在如火如荼的开展,代码量的繁多不得不令我们运用 git 这个有用的工具去管理我们共同协作的代码 git 在这里不作什么介绍了,百度一大堆的教程 首推廖雪峰老师的:http://www.liaox ...

  9. animateBackground-plugin

    (function ($) { if (!document.defaultView || !document.defaultView.getComputedStyle) { var oldCurCSS ...

  10. Node.js abaike图片批量下载爬虫1.02

    //====================================================== // abaike图片批量下载爬虫1.02 // 用最近的断点续传框架改写原有1.01 ...