angular2是否支持嵌套状态/路由?

Sur*_*aka 43 routes angular2-routing angular

angular2是否支持嵌套状态/路由?例如,在视口中有2个链接,在点击特定链接时,它将显示一个视图,该视图还具有多个链接但特定于早期链接.

Tho*_*988 50

使用新版本的路由器,如果要使用嵌套路由,下面是一个如何定义路径的示例

{
    path: 'search',
    component: SearchComponent,
    children: [
        { path: 'results/:id',  component: ResultsComponent },
    ]
}
Run Code Online (Sandbox Code Playgroud)

并在您的SearchComponent模板中添加 <router-outlet></router-outlet>

  • NG2 Router API比我冰箱里的牛奶更快弃用. (77认同)
  • 在beta和RC期间是的,并不是说完整版本已经发布它应该是稳定的. (3认同)
  • NM这有助于...... http://blog.angular-university.io/angular-2-router-nested-routes-and-nested-auxiliary-routes-build-a-menu-navigation-system/ (2认同)

Nam*_*mek 25

是.

我做了一些演示:http : //plnkr.co/edit/IcnEzZ0WtiaY5Bpqrq2Y?p=preview

import {Component, View, Input} from 'angular2/core';
import {
    RouteConfig, Router, RouteParams, ROUTER_DIRECTIVES
} from 'angular2/router';
import {PersistentRouterOutlet} from './persistent-router-outlet';


@Component({})
@View({
  template: 'product info here'
})
class ProductInfo {
}

@Component({})
@View({
  template: 'buy here'
})
class ProductBuy {
}


@Component({})
@View({
  directives: [...ROUTER_DIRECTIVES, PersistentRouterOutlet],
  template: `
    <div>
      <h2>Product {{pid}}</h2>
      <a [routerLink]="['Info']">Show Info</a>
      <a [routerLink]="['Buy']">Go Buy</a>
      <div>
        <router-outlet></router-outlet>
      </div>
    </div>
  `
})
@RouteConfig([
  {path: '/info', name: 'Info', component: ProductInfo, useAsDefault: true}
  {path: '/buy', name: 'Buy', component: ProductBuy}
])
class Product {
  pid
  constructor(params: RouteParams) {
    this.pid = params.get('pid')
  }
}

@Component({})
@View({
  directives: [...ROUTER_DIRECTIVES],
  template: `
    info about the store
  `
})
class StoreInfo {
}


@Component({
  selector: 'my-app',
  providers: [],
  directives: [...ROUTER_DIRECTIVES, PersistentRouterOutlet] ,
  template: `
    <div>
      <a [routerLink]="['./StoreInfo']">See Store Info</a>
      <a [routerLink]="['./Product', {pid:1}]">See Product 1</a>
      <a [routerLink]="['./Product', {pid:2}]">See Product 2</a>
      <div>
        <persistent-router-outlet></persistent-router-outlet>
      </div>
    </div>
  `
})
@RouteConfig([
  {path: '/', name: 'StoreInfo', component: StoreInfo, useAsDefault: true}
  {path: '/product/:pid/...', name: 'Product', component: Product}
])
export class App {
}
Run Code Online (Sandbox Code Playgroud)

这是doc:https://angular.io/docs/ts/latest/guide/router.html#!# child- router

请注意持久性选项卡存在问题:Angular2路由:持久路由选项卡和子路由 https://github.com/angular/angular/issues/6634