Routing Angular2有没有办法从路由器中获取路由列表?
我想做的是通过遍历Angular2中配置的路由列表来动态构建导航。我似乎在路由器中找不到任何可以访问已配置路由的地方。有人试过这样的吗 我查看了Routing Angular2有没有办法从路由器中获取路由列表?,routing,angular,Routing,Angular,我想做的是通过遍历Angular2中配置的路由列表来动态构建导航。我似乎在路由器中找不到任何可以访问已配置路由的地方。有人试过这样的吗 我查看了路由器的注册表属性,但它似乎没有任何可用的内容 @Component({ selector: 'my-app' }) @View({ directives: [ROUTER_DIRECTIVES, CORE_DIRECTIVES], template: ` <h1>Routing Example<
路由器
的注册表
属性,但它似乎没有任何可用的内容
@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){
}
}
@组件({
选择器:“我的应用程序”
})
@看法({
指令:[路由器指令,核心指令],
模板:`
路由示例
主菜单:
家|
一|
两个
`
})
@线路图([
{路径:'/',重定向到:'/home'},
{path:'/home',as:'home',component:Main},
{path:'/one',as:'one',component:one},
{path:'/two',as:'two',component:two},
])
导出类MyApp{
构造函数(公共位置:位置,公共路由器:路由器){
}
}
我还需要动态生成链接。据我所知,问题是路由器并没有方法生成链接,而不是手动将它们放入[router link]
中。然而有人在排队等待路由器,希望他们能尽快添加(
现在我完成了这项工作-我将routerConfig放在装饰器之外,这样我就可以在这个组件中使用它(如果我导出它,可能还有其他组件):
让routeConfig=[
{路径:'/',名称:'Intro',组件:IntroRouteComponent,useAsDefault:true},
...
{路径:'/projects',名称:'projects',组件:ProjectsRouteComponent},
{路径:'/about',名称:'about',组件:AboutRouteComponent},
];
@组成部分({
指令:[路由器指令],
提供者:[],
选择器:“应用程序”,
模板:`
{{prevRoute}}
{{nextRoute}}
`,
})
@RouteConfig(RouteConfig)
导出类AppComponent{
私人nextRoute:任何;
私人路线:任何;
构造函数(专用路由器:路由器){
这是路由器订阅(路由=>{
设i=routeConfig.findIndex(r=>r.path==('/'+route));
this.prevRoute=routeConfig[i-1]?routeConfig[i-1]。名称:false;
this.nextRoute=routeConfig[i+1]?routeConfig[i+1]。名称:false;
});
}
back(){
this._router.navigate(数组(this.prevRoute));
}
forward():任何{
this._router.navigate(数组(this.nextRoute));
}
}
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));
}
}