Angular 6 - Get current route and it's data

jedion picture jedion · Jun 15, 2018 · Viewed 31.2k times · Source

How do you able to get current route you're in and get's it's data, children and it's parent?

say if this is the route structure:

If I am currently in CompanyComponent, how do I get my current route w/c is Company, get it's parent w/c is about, it's data and it's siblings such as mission, etc.?

Answer

Sid picture Sid · Jul 31, 2018
@Component({...})
export class CompanyComponent implements OnInit {

constructor(
  private router: Router,
  private route: ActivatedRoute
) {}

ngOnInit() {

  // Parent:  about 
  this.route.parent.url.subscribe(url => console.log(url[0].path));

  // Current Path:  company 
  this.route.url.subscribe(url => console.log(url[0].path));

  // Data:  { title: 'Company' } 
  this.route.data.subscribe(data => console.log(data));

  // Siblings
  console.log(this.router.config);
}
}