I am working on the frontend of a Web Application, with Angular. I created a page that can be accessed from different other pages. From this new page, I can access other pages. I've added a "cancel" button on this page: this button should bring me back to the page I come from. So, for example, the solution provided by How to go back last page doesn't work, because I could have accessed other pages from my page, before I want to go back. So, to solve this, I created my own method (see the code below): this is based on the history of the pages that I visited before. However, this sometimes doesn't work, because the history is deleted sometimes. Is there any solution? Thanks!
cancelForm(): void {
console.log(this.routingState.getHistory());
var count = 1;
var previousUrl = this.routingState.getHistory()[this.routingState.getHistory().length - count];
while(previousUrl && (previousUrl.includes('contract-edit/add') || previousUrl.includes('project-add') || previousUrl.includes('employee-edit'))) {
previousUrl = this.routingState.getHistory()[this.routingState.getHistory().length - count];
count ++;
}
console.log(previousUrl);
this.router.navigate([previousUrl]);
}
EDIT:
To explain the situation in a better way, I give this example. I have a page containing the details of a certain project of a given company. This page can be accessed from both the page containing the list of all projects, and the list of projects for a certain client of the company. So, when I click on "cancel", I have to go back to the correct page I come from. However, from the page with the details of the project, I can also access the page with the details of a task related to this project. So I can't just use this._location.back()
for my "cancel" button, as suggested in How to go back last page, because it could bring me to the page with the task's details, if I've accessed it.