• navigate


    一、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能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

  • 相关阅读:
    导出大智慧L2要密码的公式
    SQL Server 索引结构及其使用
    职业式证券交易全貌向职业交易员进军者鉴[转]
    sqlite多字段拼接方法
    推荐两个UI、PSD文件搜索网站
    利用事件冒泡和阻止事件冒泡的例子
    js通过八个点 拖动改变div大小
    匿名函数运用js脚本一对圆括号
    js对象转换为json格式的jquery辅助类
    简单清晰的缓冲运动框架
  • 原文地址:https://www.cnblogs.com/jayruan/p/7674590.html
Copyright © 2020-2023  润新知