Get active route data inside router.events
Try this:
import { ActivatedRoute } from '@angular/router';
@Component({
...
})
export class TestComponent implements OnInit{
constructor(protected route: ActivatedRoute) {}
ngOnInit() {
let name = this.route.snapshot.params['name'];
}
}
This injects the current active route, and latter you can get info from that route.
this.router.events.subscribe(event =>
{
console.log(this.router.routerState.root);
console.log(this.router.routerState.root.firstChild);
console.log(this.router.routerState.root.firstChild && this.router.routerState.root.firstChild.firstChild);
});
merge(of(this.route.snapshot.params), this.router.events.pipe(
filter((event) => event instanceof NavigationEnd),
map(() => this.route),
map((route) => {
while (route.firstChild) route = route.firstChild;
return route;
}),
filter((route) => route.outlet === 'primary'),
mergeMap(route => route.params),
)).subscribe(p => {})