how to destroy a route in Angular?

0

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.

    
asked by Muriano 28.03.2017 в 17:42
source

1 answer

0

You can control that with the canActivate function, check that if you are logged in and the new path is the login, redirect to 404 or home ...

You can also do it through the event on the outlet router that renders the login, take a look at this link:)

  

link

    
answered by 04.04.2017 в 19:08