How to get current route
#1
I've been struggling with Angular's routing system when trying to understand how to get information about the parent route of a current route segment. The official Angular documentation covers retrieving route parameters extensively through ActivatedRoute, but it doesn't provide clear guidelines on how to access the hierarchy of route segments themselves, which is critical for breadcrumb or navigation-related tasks.
Say, for example, I am in a child component and I wish to access properties or params from its parent route. The ActivatedRoute service allows me to subscribe to a variety of Observables to get information about the current route, but I can't find an explicit method or property that directly gives access to the parent's data.
Here’s a short sample where I access the current route’s parameters:

Code:
Component,
    OnInit
} from '@angular/core';
@Component({
    selector: 'app-child',
    template: '<p>Child Component</p>'
})
export class ChildComponent implements OnInit {
    constructor(private route: ActivatedRoute) {}
    ngOnInit() {
        this.route.params.subscribe(params => {
            console.log(params);
        });
    }
}

In this scenario, I can get the params but not the actual parent segment. Does anyone know how to retrieve the parent route segment information within a component?
Reply
#2
You can access the parent route segment information by using the `parent` property of an `ActivatedRoute` instance. This can be done within your component by injecting the ActivatedRoute service and then subscribing to the parent's params or data. Check out this code where I get the parent route params:

Code:
Component,
    OnInit
} from '@angular/core';
@Component({
    selector: 'app-child',
    template: '<p>Child Component</p>'
})
export class ChildComponent implements OnInit {
    constructor(private route: ActivatedRoute) {}
    ngOnInit() {
        this.route.parent.params.subscribe(parentParams => {
            console.log(parentParams);
        });
    }
}

Remember that you can chain the `parent` property to climb up the route tree as far as you need.
Reply
#3
Sure, that works for accessing parent route parameters. But it's worth mentioning that you can also get other properties of the parent, such as data or the URL segments. Here’s how to get the parent route's data:

Code:
Component,
    OnInit
} from '@angular/core';
@Component({
    selector: 'app-child',
    template: '<p>Child Component</p>'
})
export class ChildComponent implements OnInit {
    constructor(private route: ActivatedRoute) {}
    ngOnInit() {
        this.route.parent.data.subscribe(parentData => {
            console.log(parentData);
        });
    }
}

And to get the full URL segments from the root to the current route:

Code:
Component,
    OnInit
} from '@angular/core';
import {
    Observable
} from 'rxjs';
import {
    Route,
    UrlSegment
} from '@angular/router';
@Component({
    selector: 'app-child',
    template: '<p>Child Component</p>'
})
export class ChildComponent implements OnInit {
    constructor(private route: ActivatedRoute) {}
    ngOnInit() {
        const urlSegments: Observable < UrlSegment[] > = this.route.pathFromRoot.map((r: ActivatedRoute) => r.snapshot.url).reduce((acc, e) => acc.concat(e));
        urlSegments.subscribe(segments => {
            console.log('URL Segments from root:', segments);
        });
    }
}

By using `pathFromRoot`, you are given an array of `ActivatedRoutes` from the root to the current one. You can map those to their respective URL segments and then flatten the array to get your breadcrumbs or URL hierarchy.
Reply
#4
Following the discussions above, this would be the complete component class, including necessary imports from Angular.

Code:
Component,
    OnInit
} from '@angular/core';
import {
    ActivatedRoute,
    UrlSegment
} from '@angular/router';
import {
    Observable
} from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/reduce';
@Component({
    selector: 'app-child',
    template: '<p>Child Component</p>'
})
export class ChildComponent implements OnInit {
    constructor(private route: ActivatedRoute) {}
    ngOnInit() {
        // Parent route parameters
        this.route.parent.params.subscribe(parentParams => {
            console.log('Parent Params:', parentParams);
        });
        // Parent route data
        this.route.parent.data.subscribe(parentData => {
            console.log('Parent Data:', parentData);
        });
        // URL segments from root to current route
        const urlSegments: Observable < UrlSegment[] > = this.route.pathFromRoot
            .map((r: ActivatedRoute) => r.snapshot.url)
            .reduce((acc, e) => acc.concat(e));
        urlSegments.subscribe(segments => {
            console.log('URL Segments from root:', segments);
        });
    }
}

Ensure to have the correct version of Angular and RxJS since the syntax may vary depending on the version.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)