Currently I'm practicing with a login that is like the "Home" of the application and I see myself in need, that when a user identifies he can not return to the login component (because it was already verified), and redirect him to your user "Home".
To login and restrict some of the routes I had I did with
"CanActivate".
import { Injectable } from '@angular/core';
import{CanActivate,ActivatedRouteSnapshot,RouterStateSnapshot} from '@angular/Router';
import {Observable} from 'rxjs/Rx';
import {Router} from '@angular/Router';
import {UsuariosService} from './usuarios.service';
@Injectable()
export class CanActivateTeam implements CanActivate {
private activado:boolean=false;
constructor(private _UsuariosService:UsuariosService,private _Router:Router) { }
canActivate(
route:ActivatedRouteSnapshot,
state:RouterStateSnapshot):Observable<boolean> | boolean{
this._UsuariosService.Evento.subscribe((resul)=>{
this.activado=resul});
if(this.activado){
return this.activado;
}else{
this._Router.navigate(['/login']);
}
return this.activado;
}
}
I thought that deleting some route could be done with "CanDeactivate" but it is only when a component is destroyed.