是的。有一种方法可以实现 Auth 保护。下面是例如:
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, Router } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (condition)) {
return true;
}
this.router.navigate(['/unauthorized']);
return false;
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
}
在您的路线中:
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from 'Your Location/auth-guard.service';
const ROUTES: Routes = [
{
path: '',
canActivateChild: [AuthGuard],
children: [
{path: '', component: CardbrandComponent}
]
}
];