Files
doneit-web/src/app/services/route.service.ts
T
Peter Maquiran 73354e00af improve go back
2022-01-06 14:47:22 +01:00

53 lines
999 B
TypeScript

import { Injectable } from '@angular/core';
import { NavigationEnd, Router, NavigationStart } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class RouteService {
history: any = [];
isGoBack = false;
constructor(
private router: Router,
) {
this.router.events.forEach((event) => {
if (event instanceof NavigationEnd) {
const lastRoute = this.history.slice(-1)
if(event.url != lastRoute) {
this.history.push(event.url)
}
}
});
window['RouteService'] = this
}
/**
* @param url [string] incase no history to go back
* @param option [Object] some options to the url
*/
goBack(url = null, option: object = {}) {
if(this.history.length >= 2) {
this.history.pop();
const goTo = this.history.pop();
this.isGoBack = true;
this.router.navigate([goTo]);
} else if(url) {
this.goTo({url})
}
}
goTo({url}) {
this.router.navigate([url]);
}
}