DEV Community

Midhul P
Midhul P

Posted on

Angular Back Button Made Easy with ngx-navigate-back

While browsers provide a native back button, managing navigation history within Angular applications can be tricky, ngx-navigate-back simplifies this by providing a lightweight solution to track navigation history and implement a “back” functionality — just like the browser’s back button, but within your Angular app.

did you know! 70% users expect a functional back button

What is ngx-navigate-back?

ngx-navigate-back is a librray which is easy to implement a “back” functionality in your Angular app, allowing users to return to the previous page seamlessly. Unlike traditional browser navigation, this library records navigation history within your app, giving you full control over the navigation experience.

Why Use ngx-navigate-back?

Simple Integration — Easily integrate into any Angular project.
Navigation History Tracking — Automatically records and retrieves navigation history.
Seamless Back Navigation — Navigate back with a single function call.
Retrieve Last Visited URL — Get the last visited URL for display or custom logic.

How to Use ngx-navigate-back

Install the Package
Install ngx-navigate-back using npm:

npm install ngx-navigate-back
Enter fullscreen mode Exit fullscreen mode

Import the Service
Import NgxNavigateBackService in your Angular component:

import { NgxNavigateBackService } from 'ngx-navigate-back';
Enter fullscreen mode Exit fullscreen mode

Inject the Service & Start Recording History
In your component constructor, inject the service and start tracking navigation history:

constructor(public navigation: NgxNavigateBackService) {
  this.navigation.recordUrlHistory();
}
Enter fullscreen mode Exit fullscreen mode

Implement the Back Button
Use the navigateBack() function to navigate back and getLastUrl() to retrieve the last visited URL:

<button (click)="navigation.navigateBack()">Back to Previous Page {{ navigation.getLastUrl() }}</button>
Enter fullscreen mode Exit fullscreen mode

Get Started with ngx-navigate-back

Improve user experience in your Angular app with ngx-navigate-back
Improve user experience in your Angular app with ngx-navigate-back.

Get started now! Install the package and explore its features:
🔗 ngx-navigate-back on GitHub


Let me know if you need any modifications or additional details! 🚀

If you found this helpful, let’s connect!
🔹 Follow me on GitHub for more open-source projects: GitHub
🔹 Follow me on LinkedIn to stay updated: LinkedIn


This article is Originally posted on Medium: Read here

Top comments (0)