我想要做的是通过迭代Angular2中已配置路由的列表来动态构建我的导航.我似乎无法在路由器中找到可以访问已配置路由的任何位置.有没有人尝试过这样的事情?
我查看了它Router的registry属性,但似乎没有任何可用的东西.
@Component({
    selector: 'my-app'
})
@View({
    directives: [ROUTER_DIRECTIVES, CORE_DIRECTIVES],
    template: `
        <h1>Routing Example</h1>
        <div>
            <div>
                <b>Main menu: </b>
                <a [router-link]="['Home']">Home</a> | 
                <a [router-link]="['One']">One</a> | 
                <a [router-link]="['Two']">Two</a>
                <!-- 
                  // I would rather do something like this:
                  <a *ng-for="#route of router.routes" [router-link]="['route.name']">{{ route.name }}</a>
                -->
            </div>
            <div>
                <router-outlet></router-outlet>
            </div>
        </div>
    `
})
@RouteConfig([
    { path: '/', redirectTo: '/home' },
    { path: '/home', as: 'Home', component: Main },
    { path: '/one', as: 'One', component: One },
    { path: '/two', as: 'Two', component: Two },
])
export class MyApp {
    constructor(public location: Location, public router: Router){
    }
}
我还需要动态生成链接。据我了解,问题在于路由器没有其他方法来生成链接,而是手动将它们放入[router-link]。但是... 但是他们计划添加它们。路由器的队列中有很多功能,希望它们能尽快添加(
现在,我完成了这项工作-将routerConfig放在了装饰器的外面,因此可以在此组件中使用它(如果导出,也可以在其他组件中使用):
let routeConfig = [
  { path: '/', name: 'Intro', component: IntroRouteComponent, useAsDefault: true },
  ...
  { path: '/projects', name: 'Projects', component: ProjectsRouteComponent },
  { path: '/about', name: 'About', component: AboutRouteComponent },
];
@Component({
  directives: [ROUTER_DIRECTIVES],
  providers: [],
  selector: 'app',
  template: `
    <a (click)="back()" *ngIf="prevRoute">{{ prevRoute }}</a>
    <a (click)="forward()" *ngIf="nextRoute">{{ nextRoute }}</a>
  `,
})
@RouteConfig(routeConfig)
export class AppComponent {
  private nextRoute: any;
  private prevRoute: any;
  constructor(private _router: Router) {
    this._router.subscribe(route => {
      let i = routeConfig.findIndex(r => r.path === ('/' + route));
      this.prevRoute = routeConfig[i - 1] ? routeConfig[i - 1].name : false;
      this.nextRoute = routeConfig[i + 1] ? routeConfig[i + 1].name : false;
    });
  }
  back() {
    this._router.navigate(Array(this.prevRoute));
  }
  forward(): any {
    this._router.navigate(Array(this.nextRoute));
  }
}