mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 13:26:08 +00:00
improve go back
This commit is contained in:
@@ -6,7 +6,10 @@ import { Router, NavigationEnd } from '@angular/router'
|
||||
export class NavigationService {
|
||||
private history: string[] = []
|
||||
|
||||
constructor(private router: Router, private location: Location) {
|
||||
constructor(
|
||||
private router: Router,
|
||||
private location: Location
|
||||
) {
|
||||
this.router.events.subscribe((event) => {
|
||||
if (event instanceof NavigationEnd) {
|
||||
this.history.push(event.urlAfterRedirects)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { RouteService } from './route.service';
|
||||
|
||||
describe('RouteService', () => {
|
||||
let service: RouteService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(RouteService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
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]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user