Get last part of Angular url

I need to get the last part of an URL, for example edit from /item/:id/edit. I tried using this solution from Stack Overflow but it only returns /item/:id, not the last part.

How can I get the last part of the URL?


To get the last part of an URL in Angular, you can use the split() method. Here’s an example of how to do it:

import { ActivatedRoute } from '@angular/router';

@Component({
  // component details
})
export class YourComponent implements OnInit {

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    const url = this.route.snapshot.url[0].path;
    const lastPart = url.split('/').pop();
    console.log(lastPart);
  }

}

This code snippet will log the last part of the URL to the console.