[Fixed] How to determine previous page URL in Angular?


Suppose I am currently on the page which has the URL /user/:id . Now from this page I navigate to next page :id/posts.

Now Is there a way, so that i can check what is the previous URL, i.e. /user/:id.

Below are my routes

export const routes: Routes = [
    path: 'user/:id', component: UserProfileComponent
    path: ':id/posts', component: UserPostsComponet 


You can subscribe to route changes and store the current event so you can use it when the next happens

previousUrl: string;
constructor(router: Router) {
  .pipe(filter(event => event instanceof NavigationEnd))
  .subscribe((event: NavigationEnd) => {
    console.log('prev:', event.url);
    this.previousUrl = event.url;

See also How to detect a route change in Angular?

Leave a Reply

(*) Required, Your email will not be published