Skip to content

Instantly share code, notes, and snippets.

@gildniy
Created December 12, 2017 02:59
Show Gist options
  • Save gildniy/761f98bc7e13ec48c4de0f33a4ee312e to your computer and use it in GitHub Desktop.
Save gildniy/761f98bc7e13ec48c4de0f33a4ee312e to your computer and use it in GitHub Desktop.
Route segment Angular 4 service
import { Injectable } from '@angular/core';
import { PRIMARY_OUTLET, Router, UrlSegment, UrlSegmentGroup, UrlTree } from '@angular/router';
@Injectable()
export class RouteSegment {
constructor(private router: Router) {
}
path(position: number): string {
// REF: https://angular.io/api/router/UrlSegment
const tree: UrlTree = this.router.parseUrl(this.router.url);
const g: UrlSegmentGroup = tree.root.children[PRIMARY_OUTLET];
const s: UrlSegment[] = g.segments;
return s[position].path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment