I've been following some courses and reading through various blogs covering of Routing in Angular 2. The goBack method on the Location service is one way a user can navigate to a previously viewed page (along with the browser's back button).
My question is, what can be done to prevent the user leaving the application itself. The Routing tutorial suggests the use of the CanDeactive guard. I've done some reading on this but haven't been able to understand the concept fully.
For example, I could have a simple component that includes the goBack() method. What would be the best method to prevent the user for leaving the application (through the use of the goBack method) by disabling the goBack method, while also considering they may not always enter the application on the same point. If that makes sense.
import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { Router, ActivatedRoute, Params } from '@angular/router';
@Component({
selector: 'my-app',
template: `
<h2>Angular Routing</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus laoreet ipsum massa, eget vulputate risus blandit sed. Vestibulum ullamcorper leo sagittis massa aliquet mattis.</p>
<button (click)="goBack()">❮❮ BACK</button>
<router-outlet></router-outlet>
`
})
export class AppComponent {
constructor (
private _location: Location,
private _activatedRoute: ActivatedRoute,
private _router: Router
) { }
goBack(): void {
this._location.back();
}
}
This may not be possible but it has got me thinking.