如何在Angular 2中使用子路由

Pra*_*hag 14 angular-routing angular

Angular 2版本:2.0.0-alpha.44

我一直在尝试在Angular 2中进行路由.虽然我能够完成正常的路由,但是当我介绍子路由时,我遇到了一些问题.以下是plnkr上的示例(http://plnkr.co/edit/Y5doqU1WcEe4Ldr7KfPJ)

以下是快速参考的代码.我正在努力实现以下路由

                                  App
                                  /\
                                 /  \
                             HomeCmp HelloCmp
                                      \
                                       \
                                     ChildCmp
Run Code Online (Sandbox Code Playgroud)

下面是我如何配置我的路径

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)
]);
Run Code Online (Sandbox Code Playgroud)

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

EXCEPTION: Link "["HelloCmp"]" does not resolve to a terminal or async instruction. in [null]
Run Code Online (Sandbox Code Playgroud)

我按照这里提到的文章.但是无法让它发挥作用.有人可以帮忙吗?谢谢

Par*_*ain 11

你只需要导入子组件routerLink然后你的代码工作正常.例如:

<a [routerLink]="['./HelloCmp','ChildCmp']">hello</a>
Run Code Online (Sandbox Code Playgroud)

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

有关此路由的详细信息请参见GitHub上这个问题在这里

更新到Angular 2 beta

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

  • router-link 已更改为routerLink

  • 你也可以useAsDefault : true在routerLink这样的时候使用而不是提供孩子 -

    {path:'/ blabla',components:ABC,name:ABC,useAsDefault:true}


Bal*_*wal 5

Angular4子路由 -

让我首先定义路由配置,每个路由都可以有一个名为children的属性,您可以在其中定义此路由的子路由.

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}
];
Run Code Online (Sandbox Code Playgroud)
  1. 如果用户导航到说/ artist/1234,它将重定向到/ artist/1234/tracks.
  2. 此路线匹配/ artist/1234/tracks等网址.
  3. 此路线匹配/ artist/1234/albums等网址.
<h1>Artist</h1>
<p>
  <a [routerLink]="['./tracks']">Tracks</a> |
  <a [routerLink]="['./albums']">Albums</a>
</p>
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)