Angular 5 - Which Should I Use To Navigate Backward - Href Or Location.back()?
I have an app of which one can navigate to a page in which there is no way forwards. Think of clicking a program in a TV guide to open a page with that program's details, obviously
Solution 1:
You should use built-in Location service of Angular as.
import {Component} from'@angular/core';
import {Location} from'@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
classAppComponent {
constructor(private location: Location) {
}
back() {
this.location.back();
}
}
Solution 2:
If you want to navigate in an Angular app, you don't want to use classic href
links since they are designed to work for server-rendered apps, but not client-side apps that use the History Javascript API.
Prefer using the RouterLink from Angular.
Between RouterLink
and location.back()
the choice is yours, it depends whether you want to control the page you want to redirect to, or just want the same behavior as the back button from your browser.
Post a Comment for "Angular 5 - Which Should I Use To Navigate Backward - Href Or Location.back()?"