I'm trying to fetch the current route of my application and in this article on Angular's doc site they have this code
@Component({
selector: 'app-routable',
template: 'Routable2Component template'
})
export class Routable2Component implements OnInit {
constructor(private activatedRoute: ActivatedRoute) {}
ngOnInit() {
this.activatedRoute.url
.subscribe(url => console.log('The URL changed to: ' + url));
}
}
So in my own code I do this
CurrentPage!: string;
constructor(
private currentRoute: ActivatedRoute
){}
ngOnInit(): void {
this.currentRoute.url.subscribe(a => {
this.CurrentPage = a;
});
}
However things didn't behave the way I thought they would so I commented out the assignment and did console.log(a)
and saw the url
is an array. Inside this array there's an object with a path
property which is empty. Am I missing something here? How do I get this to work as the doc pages suggest?