【问题标题】:RouteParameter in angular2 RC1 CLIangular2 RC1 CLI 中的路由参数
【发布时间】:2016-06-25 06:31:30
【问题描述】:

我正在使用 RC.1 在 angular2 CLI 中进行路由,但似乎在发布后路由有很多变化 angular2 RC,在此之前,在 angular2 beta 中一切正常,

  1. 如果我在 routerLink 中使用 ../ 来使用路由器,将查看当前组件的父级 angular throws 错误

    Invalid number of '../'

如何在 RC 中使用子路由?

  1. 点击按钮时路由不起作用
  2. Routerparams 在 angular2 RC.1 中不起作用

有什么想法吗?

【问题讨论】:

    标签: angular routing


    【解决方案1】:

    搜索后解决它可能对其他人有帮助,作为答案发布-

    错误Invalid number of '../'的解决方法

    您需要导入 RouteSegment 并将其添加到您的构造函数中

    import { Router, RouteSegment } from '@angular/router';
    ...
    contructor(private $_router:Router, private $_segment:RouteSegment){}
    ...
    
    //If you are in for example child1 and child 1 is sibling of child2
    this.$_router.navigate(['../child2'],this.$_segment);
    

    Credit goes to this answer

    angular2 RC 中的子路由

    没有什么/... 像在 angular2 beta 中指定有子路由,你只需要声明路由就不需要使用这个/...

    Routerparams 在 angular2 RC.1 中不起作用

    现在RouteParams 已更改为RouteSegment,因此您必须将RouteSegment 引入构造函数并获取这样的参数 -

    constructor(params: RouteSegment) {
            let id = params.getParam("params_name");
        }
    

    【讨论】:

      【解决方案2】:

      RouteParams 的答案:

      你必须为路由添加参数:

      @Routes([  
        {path: '/my-component/:id', component: MyComponent}
      ])
      

      在RouterLink中使用逗号而不是对象:

      [routerLink]="['/my-component',id]"
      

      并访问组件中参数的值:

      constructor(curr: RouteSegment) {
              let itemID = curr.getParam("page_id");
          }
      

      【讨论】:

      • 那么 angular2 RC 中的子路由呢?
      猜你喜欢
      • 1970-01-01
      • 2017-12-03
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      相关资源
      最近更新 更多