This commit is contained in:
Peter Maquiran
2021-07-27 22:34:47 +01:00
parent 2cb96a80ac
commit 999da281e8
5 changed files with 91 additions and 33 deletions
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { NavigationService } from './navigation.service';
describe('NavigationService', () => {
let service: NavigationService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(NavigationService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+26
View File
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core'
import { Location } from '@angular/common'
import { Router, NavigationEnd } from '@angular/router'
@Injectable({ providedIn: 'root' })
export class NavigationService {
private history: string[] = []
constructor(private router: Router, private location: Location) {
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.history.push(event.urlAfterRedirects)
}
})
}
back(): void {
this.history.pop()
if (this.history.length > 0) {
this.location.back()
} else {
this.router.navigateByUrl('/')
}
}
}