Angular 2 version: 2.0.0-alpha.44
I've been trying to do routing in Angular 2. Though i was able to do the normal routing done, i am facing some issues when i introduce child routes. Here is the example on plnkr (http://plnkr.co/edit/Y5doqU1WcEe4Ldr7KfPJ)
Below is the code for quick reference. I am trying to achieve below routing
App
/\
/ \
HomeCmp HelloCmp
\
\
ChildCmp
And below is how i've configured my paths
import {bootstrap, bind, Component, View} from 'angular2/angular2'
import {RouteConfig, RouteParams, ROUTER_DIRECTIVES, APP_BASE_HREF, ROUTER_BINDINGS} from 'angular2/router'
@Component({
selector: 'child-cmp'
})
@View({
template: `
<div>
<h3>Whats up</h3>
</div>
`
})
class ChildCmp { }
// ************************ Hello Component ***********************************
@Component({
selector: 'hello-cmp'
})
@View({
template: `
<div>
<h2>Hello there !</h2>
<router-outlet></router-outlet>
</div>
`,
directives: ROUTER_DIRECTIVES
})
@RouteConfig([
{path: '/', component: ChildCmp, as: 'ChildCmp'}
])
class HelloCmp { }
//************************** HOME Component ***********************************
@Component({
selector: 'home-cmp'
})
@View({
template: `
<div>
<h2>Welcome Home</h2>
</div>
`
})
class HomeCmp {}
//************************* APP Component *************************************
@Component({
selector: 'app-cmp'
})
@View({
template: `
<div>
<h1>Hello {{message}}!</h1>
<a [router-link]="['./HomeCmp']">home</a>
<a [router-link]="['./HelloCmp']">hello</a>
<hr>
<router-outlet></router-outlet>
</div>
`,
directives: ROUTER_DIRECTIVES
})
@RouteConfig([
{path: '/', component: HomeCmp, as: 'HomeCmp'}
{path: '/hello/...', component: HelloCmp, as: 'HelloCmp'}
])
export class App {
message:string = 'world';
}
bootstrap(App, [
ROUTER_BINDINGS,
bind(APP_BASE_HREF).toValue(location.pathname)
]);
When i remove child routes it works fine. But with child routes i get below error.
EXCEPTION: Link "["HelloCmp"]" does not resolve to a terminal or async instruction. in [null]
I followed the article mentioned here. But not able to get it working. Can someone please help ? Thanks
You just need to import the child component in the routerLink
then your code is working fine. For example:
<a [routerLink]="['./HelloCmp','ChildCmp']">hello</a>
Here is the working plnkur of your code. Plunker Code
For more info about this routing see this issue on github here
As of Angular 2 beta some changes are here:
router-link
has been changed to routerLink
You can also use useAsDefault : true
instead of providing child at the time of routerLink like this -
{path: '/blabla', components: ABC , name : ABC, useAsDefault: true}