简体   繁体   English

angular2 RC1 CLI中的RouteParameter

[英]RouteParameter in angular2 RC1 CLI

I am working on routing in angular2 CLI using RC.1, but it seems there are many changes in routing after the release of angular2 RC, before that in angular2 beta everything works fine, 我正在使用RC.1在angular2 CLI中进行路由,但是在angular2 RC发布之后,似乎在路由中进行了许多更改,在此之前,在angular2 beta中一切正常,

  1. If I use ../ in routerLink for using router will look at the current component's parent angular throws error 如果我在routerLink中使用../来使用router,则会查看当前组件的父角度抛出错误

    Invalid number of '../'

How to use child routing in RC? 如何在RC中使用子路由?

  1. Routing not working on Button click 路由无法在按钮上单击
  2. Routerparams is not working in angular2 RC.1 Routerparams在angular2 RC.1中不起作用

any idea? 任何想法?

Solve it after searching may help someone else, posting as answer- 搜索后解决该问题可能会帮助其他人,并发布为答案-

Solution of error Invalid number of '../' 错误的解决方案Invalid number of '../'

You need to import RouteSegment and add it you your constructor 您需要导入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 功劳归功于这个答案

Child Routing in angular2 RC angular2 RC中的子路由

There is nothing /... like in angular2 beta which specify having Child route, you just have declare routing simply no need to use this /... 没有什么/...像在angular2 beta中那样指定具有子级路由,您只需要声明路由就根本不需要使用此/...

Routerparams is not working in angular2 RC.1 Routerparams在angular2 RC.1中不起作用

Now RouteParams has been changed to RouteSegment so you have to intilize RouteSegment into the constructor and get params like this - 现在, RouteParams已更改为RouteSegment因此您必须将RouteSegment引入构造函数中,并获得如下所示的参数-

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

Answer for the RouteParams: 回答RouteParams:

you have to add parameters to route: 您必须添加参数以进行路由:

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

use parameter in RouterLink with comma not with object: 在RouterLink中将参数与逗号而不与对象一起使用:

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

and access the value of parameter in your component : 并访问组件中parameter的值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM