简体   繁体   English

Angular 2获取父路线

[英]Angular 2 Get parent routes

Is there anyway to get a collection of the parent routes with a provided activated route? 无论如何,要使用提供的激活路由来获取父路由的集合?

If I have a nested route structure like: 如果我有一个嵌套的路由结构,例如:

[
    {
        path: 'search', component: SearchComponent, children:
        [
            {
                path: 'view/:id', component: ViewSearchComponent, children:
                [
                    { path: 'person/:id', component: PersonComponent }
                ]
            },
        ]
    }
]

My url will look like this: /search/view/3/person/5 . 我的网址将如下所示: /search/view/3/person/5

How can I turn that into some sort of structure like: 我如何将其转换为某种结构,例如:

[
  { part: 'search' }
  { part: 'view/3' }
  { part: 'person/5' }
]

I've looked at the URLTree and URLSegments, but that looks like it won't be able to differentiate between what's the parameters and what's the path. 我看过URLTree和URLSegments,但是看起来它无法区分参数是什么和路径是什么。

you can run following code, it will return same object that you want. 您可以运行以下代码,它将返回所需的相同对象。

  import {ActivatedRoute} from '@angular/router';

  export class Component {

       constructor(private route: ActivatedRoute){
              let pathroots = this.route.pathFromRoot;
              let arr = [];
              pathroots.forEach(path => {
                  let obj: any = {};
                  let pathurl = '';
                  path.url.subscribe(url => {
                       url.forEach(e => {
                          pathurl += e + '/';
                       });
                  });
                  obj['part'] = pathurl;
                  arr.push(obj);
             });
             console.log(arr,'*******************');
           }

 }

You can inject ActivatedRoute and then iterate the parent property until there is none 您可以注入ActivatedRoute ,然后迭代parent属性,直到没有

https://angular.io/docs/ts/latest/api/router/index/ActivatedRoute-interface.html#!#parent-anchor https://angular.io/docs/ts/latest/api/router/index/ActivatedRoute-interface.html#!#parent-anchor

I came up with something on my own, thanks to Gunter. 感谢Gunter,我自己想出了一些办法。 This should be good enough to implement some sort of Breadcrumbs feature on your component. 这应该足以在您的组件上实现某种面包屑功能。 It's pretty much the same as Vikash but is a bit more "rxjs'y" 它与Vikash几乎相同,但更加“ rxjs'y”

private urlSub: Subscription;

private buildNavigationTree(): void {
    const pathFromRoot = this.activatedRoute.pathFromRoot;
    let urlSub = Observable.merge(...pathFromRoot.map(p => p.url));
    let urlList: any[] = [];
    let url;
    //TODO somehow we have to recalculate the WHOLE thing on url change
    //right now if part of the url changes i think it'll append to the end.
    //possibly use navigationend? 
    this.urlSub = urlSub.subscribe(segments => {
        //skip empty segments which show up sometimes
        if (segments.length == 0) {
            return;
        }
        url = this.buildUrl(url, segments);
        urlList.push({
            url: url
        });
    });
}

private buildUrl(url: string, segment: UrlSegment[]): string {
    return url + segment.map(s => s.path).join('/') + '/';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM