加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:03:33 所属栏目:安全 来源:网络整理
导读:我有一个快速的问题。我目前正在浏览 https://angular.io/docs/ts/latest/api/router/Router-class.html,但我想知道,在我的Angular2的main.ts中,我的路由定义如此: @Routes([ { path: '/',component: HomeComponent },{ path: '/about-me',component: Ab
我有一个快速的问题。我目前正在浏览 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);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读