Tienes que estar cituado en el directorio donde manejaras el guard src/app/_guard
En este guard se esta usando Alertify
ng g guard auth --spec=false
#Esto generara un archivo auth.guard.ts
//Ubicado en src/app/_guard
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from '../_services/auth.service';
import { AlertifyService } from '../_services/alertify.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router,
private alertify: AlertifyService) {}
canActivate(): boolean {
if (this.authService.loggedIn()) {
return true;
}
this.alertify.error('you shall not pass!!!');
this.router.navigate(['/home']);
return false;
}
}
import {Routes} from '@angular/router';
import { HomeComponent } from './home/home.component';
import { MemberListComponent } from './member-list/member-list.component';
import { AuthGuard } from './_guards/auth.guard';
export const appRoutes: Routes = [
{ path: 'home', component: HomeComponent},
{ path: 'members', component: MemberListComponent, canActivate: [AuthGuard]}, //Esta es la forma de proteget una unica ruta que el usuario este autentificado
{ path: '**', redirectTo: 'home', pathMatch: 'full'}
// Protegiendo Multiples rutas
{
path: '',
runGuardsAndResolvers: 'always',
canActivate: [AuthGuard],
childer: [
{ path: '[MiPath]', component: [MiCOmponente]Component },
{ path: '[MiPath]', component: [MiCOmponente]Component },
{ path: '[MiPath]', component: [MiCOmponente]Component },
]
}
];