• angular6路由参数的传递与获取


    1.访问路由链接:/test/id

    路由配置:

    {path: 'test/:id', component: TestComponent}

    html传参:

    <a href="javascript:void(0);" [routerLink] = "['/test', '121']">...</a>

    ts传参:

    this.router.navigate(['/test', '121']);//router为Router的实例

    取参:

    this.route.snapshot.params['id'] //route为ActivatedRoute的实例

    2.访问路由链接:/test?id='121'

    路由配置:

    {path: 'test', component: TestComponent}

    html传参:

    <a href="javascript:void(0);" [routerLink] = "['/test']" [queryParams]="{id:'121'}">...</a>

    ts传参:

    this.router.navigate(['/test'],{ queryParams: { id: '121' });

    取参:

    this.route.snapshot.queryParams['id']

    3.不通过路由链接

    {
        path: '',
        component: InterviewSetComponent,
        data: {
          title: '标题',
        }
    }
    
    //取参
    
     this.route.snapshot.data.title;

    如果想获取父路由或子路由的参数,可通过this.route.snapshot.parent 或 this.route.snapshot.children去获取(具体的console.log(this.route.snapshot)去查看)

  • 相关阅读:
    组合模式
    装饰者模式
    桥接设计模式
    Docker介绍
    适配器模式
    设计软件下载
    17种设计字体的创意方法
    less编译(mac版gulp)
    50种常用快捷键整理
    WebStorm设置手机测试服务器-局域网内其他设备访问
  • 原文地址:https://www.cnblogs.com/myyouzi/p/12066467.html
Copyright © 2020-2023  润新知