Angular 2中如何使用子路由

How to use child routes in Angular 2

Angular 2 版本:2.0.0-alpha.44

我一直在尝试在 Angular 2 中进行路由。虽然我能够完成正常的路由,但在引入子路由时我遇到了一些问题。这是 plnkr (http://plnkr.co/edit/Y5doqU1WcEe4Ldr7KfPJ)

上的示例

下面是供快速参考的代码。我正在尝试实现以下路由

                                  App
                                  /\
                                 /  \
                             HomeCmp HelloCmp
                                      \
                                       \
                                     ChildCmp

下面是我配置路径的方式

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)
]);

当我删除子路由时,它工作正常。但是对于子路由,我得到以下错误。

EXCEPTION: Link "["HelloCmp"]" does not resolve to a terminal or async instruction. in [null]

我关注了提到的文章here但无法正常工作。有人可以帮忙吗?谢谢

您只需要在 routerLink 中导入子组件,您的代码就可以正常工作。例如:

<a [routerLink]="['./HelloCmp','ChildCmp']">hello</a>

这是您的代码的工作 plnkur。 Plunker Code

有关此路由的更多信息,请参阅 github here

上的此问题

更新到 Angular 2 测试版

从 Angular 2 beta 开始,这里有一些变化:

  • router-link 已更改为 routerLink

  • 你也可以使用 useAsDefault : true 而不是像这样在 routerLink 时提供 child -

    {路径:'/blabla',组件:ABC,名称:ABC,useAsDefault:true}

A​​ngular4 子路由 -

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. 如果用户导航说 /artist/1234 它将重定向到 /artist/1234/曲目.
  2. 此路线与 URL 匹配 /artist/1234/tracks.
  3. 此路线与 URL 匹配 /artist/1234/albums.
<h1>Artist</h1>
<p>
  <a [routerLink]="['./tracks']">Tracks</a> |
  <a [routerLink]="['./albums']">Albums</a>
</p>
<router-outlet></router-outlet>