Home > Software engineering >  How to get previous url in angular when current url/page is refreshed?
How to get previous url in angular when current url/page is refreshed?

Time:11-30

For example I am from the current url http://localhost:4200/#/transactions/overview/5?tab=2 and then I navigated to http://localhost:4200/#/deals/detail/

When I refresh the deals/detail page I want to go back to the previous url which is for example http://localhost:4200/#/transactions/overview/5?tab=2.

How do we address this is angular ? Thank you for help and ideas.

#code for navigating to deals/detail page

 editDeal(deal:any){
        let dealType = '';
        const state = { 
          data: {
            transaction: this.transaction,
          },      
        }
        this.gotoDealDetails(state);    
      }
    
      gotoDealDetails(state:any){
        this.route.navigateByUrl(`deals/detail/`, {state: state});
      }

#detail details page component

export class DealDetailsComponent implements OnInit {


      dealDetails = null;
      leaseDetails = null;
      currentRentSchedule = [];
      isInEditMode = false;
      fiscalYear = null;
      pmrType = null;
      partnerType: any;
    
      constructor(
        private _route: Router,    
        private _dealService: DealService,
        private dialog: MatDialog,
        private _notificationService: NotificationService) {
        this.urlData = this._route.getCurrentNavigation().extras.state.data
      }
    
      ngOnInit(): void {    
    
      }
    
      ngAfterViewInit() {
    
      }
    
    }

CodePudding user response:

When you refresh the page, get any event to be called on refresh page and in that event you can call back() method as below in your component (here taken name as AnyComponent) in which you want to perform refresh :

component.ts file example

import { Location } from '@angular/common'

    @Component({...})
    export class AnyComponent {
      constructor(private location: Location) {}

      back(): void {
        this.location.back()
      }
    }

Refer this link for more detail: https://nils-mehlhorn.de/posts/angular-navigate-back-previous-page

  • Related