I want to have back-to-the-previous page.
Previous Page: job.html Current Page: jobDetail.html
As per instructions, I have added import { Location } from '@angular/common';
to the jobDetail.component.ts
file at the top followed by
export class MyDetailComponent implements OnInit {
constructor(private location: Location) {}
ngOnInit() {
this.location.subscribe(x => console.log(x));
}
}
I have a html code in jobDetail.html but don't know how to proceed further. How do I add a previous button correctly. There's no easy tutorial for newbies like me.
<a routerLink="">Back</a>
navigate(LASTPAGE); For example, page C has a Go Back button, Page A -> Page C, click it, back to page A. Page B -> Page C, click it, back to page B.
If you intend not to update your URL parameter within the same component you are accessing it, then you can use the snapshot. As the name suggests, the parameter would only be accessed once, when the component loads. Hence, it won't be updated, even if you change its value from within the same component.
This worked as posted by Hinrich:
import { Location } from '@angular/common';
// more imports here...
@Component({
// component's declarations here
})
export class MyComponent {
constructor(private location: Location) { } // inject Location into class constructor
cancel() {
this.location.back(); // <-- go back to previous location on cancel
}
}
HTML
<a (click)="cancel()">Back</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With