Parshuram Kalvikatte
Parshuram Kalvikatte

Reputation: 1646

Go Back in Angular 6 when skiplocationchange is true

Routing in Angular 6

When i route to a particular location i navigate with

route.navigate(['/home'], { skipLocationChange: true });

but when returning back to previous route the below code is not helping, Is there any another way or should by remove "{ skipLocationChange: true }"

import {Component} from '@angular/core';
import {Location} from '@angular/common';

@Component({
  // component's declarations here
})
class SomeComponent {

  constructor(private _location: Location) 
  {}

  backClicked() {
    this._location.back();
  }
}

Upvotes: 4

Views: 5250

Answers (2)

Sherif Fahd
Sherif Fahd

Reputation: 131

you can pass dynamic data to a Route The option to pass the dynamic data or a user-defined object was added in the Angular Version +8 using the state object. The state object is stored in History API

Using routerLink directive

<a [routerLink]="['antherUrl']" [state]="{ id:1 , name:'currentUrl'}">Dynamic Data</a>

Using navigateByUrl

this.router.navigateByUrl('/antherUrl', { state: { id:1 , name:'currentUrl' } });

Accessing the state value The state can be accessed by using the getCurrentNavigation method of the router (works only in the constructor)

 perviousUrl = this.router.getCurrentNavigation().extras.state


goBack() {
 this.router.navigate([this.previousUrl.name],{skipLocationChange: true});
}

Upvotes: 1

Teddy Sterne
Teddy Sterne

Reputation: 14221

Using skipLocationChange

Navigates without pushing a new state into history.

This is why using location.back() will not work as it simply moves the browser back to the previous state in the history. And the current state is unchanged even though the url in the browser was modified.

You should not use skipLocationChange if you want the next state of the page to be added to the browser's history.

Upvotes: 5

Related Questions