<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: German Gonzalo Saracca</title>
    <description>The latest articles on DEV Community by German Gonzalo Saracca (@germansaracca).</description>
    <link>https://dev.to/germansaracca</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F937839%2Fcb054199-54c1-4917-a27f-7ee15070b3b4.jpeg</url>
      <title>DEV Community: German Gonzalo Saracca</title>
      <link>https://dev.to/germansaracca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/germansaracca"/>
    <language>en</language>
    <item>
      <title>Back Button Navigation - Angular</title>
      <dc:creator>German Gonzalo Saracca</dc:creator>
      <pubDate>Tue, 24 Oct 2023 14:33:49 +0000</pubDate>
      <link>https://dev.to/germansaracca/back-button-navigation-angular-4fal</link>
      <guid>https://dev.to/germansaracca/back-button-navigation-angular-4fal</guid>
      <description>&lt;p&gt;I will go straight to the point, in a web app where I work the need emerged to standardize the behavior of such a simple button to navigate backwards. &lt;br&gt;
Not to navigate backwards in the hierarchy of routes, for example:&lt;br&gt;
&lt;code&gt;Products =&amp;gt; Product Detail =&amp;gt; Edit Product&lt;/code&gt;&lt;br&gt;
but to navigate backwards in the navigation history that the user had done.&lt;/p&gt;

&lt;p&gt;First we thought about the simple history.go(-1), but the problem is that we didn't want to exit the web app, suppose the user arrives at a particular page of my web app and clicks the back button, I would exit the web app; and we didn´t want that.&lt;/p&gt;

&lt;p&gt;Searching the internet we realized that such a simple task was not, or at least without making the task too complex, so I'm going to share our solution, at least with Angular.&lt;br&gt;
 &lt;em&gt;*I have removed all styles to simplify the example.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;back-button.component.ts&lt;/code&gt; Our logic 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Location } from '@angular/common';
import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'back-button',
  templateUrl: './back-button.component.html',
  standalone: true,
})
export class BackButtonComponent {
  constructor(private _location: Location, private _router: Router) {}

  goBack() {
    if (window.history.state.navigationId === 1) {
      this._router.navigate(['/home']);
    } else {
      this._location.back();
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;goBack&lt;/code&gt; function, we are checking if the &lt;code&gt;navigationId&lt;/code&gt; property inside &lt;code&gt;window.history.state&lt;/code&gt; is 1, which means that we are where the user first arrived in our web app, so instead of navigating outside our app we redirect user to the dashboard, otherwise just go back once.&lt;br&gt;
&lt;strong&gt;Important&lt;/strong&gt;: This &lt;code&gt;navigationId&lt;/code&gt; is something that Angular pushes to the native state of the history and keeps incrementing it on location changes. We are using Angular 15.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;back-button.component.html&lt;/code&gt; Our simple html 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button (click)="goBack()"&amp;gt;
  Back
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To conclude, I think it is a simple solution using the help that Angular gives us to keep track in some way the count of navigations between routes in our application.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>web</category>
      <category>javascript</category>
      <category>router</category>
    </item>
  </channel>
</rss>
