Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use child routes in Angular 2

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

like image 714
Prakash Shanbhag Avatar asked Nov 21 '15 05:11

Prakash Shanbhag


People also ask

What is the use of child routes in Angular?

With child routes, you can have a component-like structure defined for the routes in your app. It is critical as there are views that the user should not be able to access unless they are in a particular view. This way, the structure becomes tree-like, just like the structure of components.

Where would you define child URL routes in Angular?

Just like we had a <router-outlet></router-outlet> for the root application component, we would have a router outlet inside the ProductDetails component. The components corresponding to the child routes of product-details would be placed in the router outlet in ProductDetails .


2 Answers

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

Update to Angular 2 beta

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}

like image 71
Pardeep Jain Avatar answered Sep 20 '22 17:09

Pardeep Jain


Angular4 child Routing -

Let me first define the route configuration, each route can have a property called children where you can define the child routes of this route.

const routes: Routes = [
  {path: '', redirectTo: 'home', pathMatch: 'full'},
  {path: 'find', redirectTo: 'search'},
  {path: 'home', component: HomeComponent},
  {path: 'search', component: SearchComponent},
  {
  path: 'artist/:artistId',
  component: ArtistComponent,
  children: [
  {path: '', redirectTo: 'tracks'}, ①
  {path: 'tracks', component: ArtistTrackListComponent}, ②
  {path: 'albums', component: ArtistAlbumListComponent}, ③
  ]
  },
  {path: '**', component: HomeComponent}
];
  1. If a user navigates to say /artist/1234 it will redirect to /artist/1234/tracks .
  2. This route matches a URL like /artist/1234/tracks.
  3. This route matches a URL like /artist/1234/albums.
<h1>Artist</h1>
<p>
  <a [routerLink]="['./tracks']">Tracks</a> |
  <a [routerLink]="['./albums']">Albums</a>
</p>
<router-outlet></router-outlet>
like image 38
Balwant Padwal Avatar answered Sep 21 '22 17:09

Balwant Padwal