简体   繁体   中英

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,

  1. If I use ../ in routerLink for using router will look at the current component's parent angular throws error

    Invalid number of '../'

How to use child routing in RC?

  1. Routing not working on Button click
  2. Routerparams is not working in angular2 RC.1

any idea?

Solve it after searching may help someone else, posting as answer-

Solution of error Invalid number of '../'

You need to import RouteSegment and add it you your constructor

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

There is nothing /... like in angular2 beta which specify having Child route, you just have declare routing simply no need to use this /...

Routerparams is not working in angular2 RC.1

Now RouteParams has been changed to RouteSegment so you have to intilize RouteSegment into the constructor and get params like this -

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

Answer for the 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]="['/my-component',id]"

and access the value of parameter in your component :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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