Reputation: 86790
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,
../
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?
any idea?
Upvotes: 0
Views: 1574
Reputation: 86790
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);
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");
}
Upvotes: 2
Reputation: 353
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");
}
Upvotes: 1