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/mon';
@Component({
// ponent's declarations here
})
class SomeComponent {
constructor(private _location: Location)
{}
backClicked() {
this._location.back();
}
}
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/mon';
@Component({
// ponent's declarations here
})
class SomeComponent {
constructor(private _location: Location)
{}
backClicked() {
this._location.back();
}
}
Share
Improve this question
asked Nov 15, 2018 at 12:56
Parshuram KalvikatteParshuram Kalvikatte
1,6464 gold badges23 silver badges45 bronze badges
2
- Have you found how to hide url and at the same time be able to go back in Angular? I need it too. :) – manymanymore Commented May 19, 2020 at 20:27
- 1 No @YaroslavTrofimov I wrote a service which saves my url history and push and pop base on Forward and Backward – Parshuram Kalvikatte Commented Jun 8, 2020 at 7:02
2 Answers
Reset to default 5Using 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.
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});
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744400758a4572373.html
评论列表(0条)