typescript – 如何在我的Angular2应用程序中列出/输出@Routes中的所有路由

前端之家收集整理的这篇文章主要介绍了typescript – 如何在我的Angular2应用程序中列出/输出@Routes中的所有路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个快速的问题。我目前正在浏览 https://angular.io/docs/ts/latest/api/router/Router-class.html,但我想知道,在我的Angular2的main.ts中,我的路由定义如此:
@Routes([
    { path: '/',component: HomeComponent },{ path: '/about-me',component: AboutMeComponent },{ path: '/food',component: FoodComponent },{ path: '/photos',component: PhotosComponent },{ path: '/technology',component: TechnologyComponent },{ path: '/blog',component:Blogomponent },])

现在在其他地方的组件中我导入了Router类。在我的组件(或组件模板)中,我想遍历我定义的所有路由或只能访问它们。有没有内置的方法来做到这一点?像一些返回对象数组的函数?这是我想要的粗略想法……

@Component({
    selector: 'ms-navigation',templateUrl: 'src/navigation/navigation.template.html',directives: [ ROUTER_DIRECTIVES ]
})

export class NavigationComponent {
    constructor(private router:Router) {   
        // what can I do here to get an array of all my routes?
        console.log(router.routes); ????
    }
}
如果您只需要路径路径作为字符串,您可以通过迭代您的Router对象的配置数组来找到它们。
for (var i = 0; i < this.router.config.length; i++) {
        var routePath:string = this.router.config[i].path;
        console.log(routePath);
    }
原文链接:https://www.f2er.com/angularjs/143986.html

猜你在找的Angularjs相关文章