How to use child routes in Angular 2

2020-01-31 03:11发布

问题:

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

回答1:

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}



回答2:

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>