首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在我的Angular2应用中列出/输出@Routes中的所有路由

如何在我的Angular2应用中列出/输出@Routes中的所有路由
EN

Stack Overflow用户
提问于 2016-06-01 21:20:24
回答 6查看 44.9K关注 0票数 67

我有一个简短的问题。我目前正在通过https://angular.io/docs/ts/latest/api/router/Router-class.html查找,但我想知道,在我的Angular2的main.ts中,我的路线定义如下:

代码语言:javascript
复制
@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类。在我的组件(或组件模板)中,我希望遍历我定义的所有路由,或者只是能够访问它们。有没有一种内置的方法可以做到这一点?像一些返回对象数组的函数?这是我想要的粗略想法。

代码语言:javascript
复制
@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); ????
    }
}
EN

回答 6

Stack Overflow用户

发布于 2017-09-11 15:30:02

显然,有一种非常简洁的方法:

代码语言:javascript
复制
constructor(private router: Router) {}

ngOnInit() {
  console.log('configured routes: ', this.router.config);
}
票数 35
EN

Stack Overflow用户

发布于 2016-11-24 02:28:15

如果只需要字符串形式的路由路径,可以通过迭代Router对象的config数组来找到它们。

代码语言:javascript
复制
    for (var i = 0; i < this.router.config.length; i++) {
        var routePath:string = this.router.config[i].path;
        console.log(routePath);
    }
票数 12
EN

Stack Overflow用户

发布于 2021-02-11 19:45:35

我正在使用这个公司。获取angular 9中的所有路由

代码语言:javascript
复制
import { Compiler, Component, Injector, OnInit } from '@angular/core';
import { Route, Router } from '@angular/router';

@Component({
  templateUrl: './sitemap.component.html'
})
export class SiteMapComponent implements OnInit {

  public urls: string[] = [];
  constructor(private _router: Router, private compiler: Compiler, private injector: Injector) {

  }

  ngOnInit() {
    this._router.config.forEach(i => {
      this.getPaths(i);
    })
  }

  getPaths(route: Route, parent: string = '') {
    if (route.redirectTo) {
      return;
    }
    if (route.children) {
      route.children.forEach(i => {
        this.getPaths(i, parent + route.path);
      });
    }
    else if (route.loadChildren) {
      (<any>this._router).configLoader.load(this.injector, route).subscribe(i => {
        i.routes.forEach(j => {
          this.getPaths(j, parent + route.path)
        });
      });
    }
    else if (route.path != null) {
      this.setPath(route.path, parent);
    }
  }
  setPath(path, parent) {
    let fullPath: string;
    if (path != null) {
      if (parent) {
        fullPath = `/${parent}/${path}`;
      }
      else {
        fullPath = `/${path}`
      }
    }
    this.urls.push(fullPath)
  }
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37569936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档