我正在尝试找到一个解决方案,该解决方案将在首先加载角度(html和组件)之前重定向到授权端点。这甚至可能吗?或者是否可以在我的主要 app.component 上使用 CanActivate auth guard?
我正在使用身份验证保护,就像任何其他角度项目一样。但它在重定向到授权端点之前首先加载 html。
这是我到目前为止所做的:
(我的路由组件)
const routes: Routes = [
{
path: '',
redirectTo: 'main',
pathMatch: 'full',
canActivate: [AdminRouteGuard]
},
{
path: 'main',
component: MainComponent,
canActivate: [AdminRouteGuard]
},
{
path: '**',
redirectTo: 'main',
pathMatch: 'full',
canActivate: [AdminRouteGuard]
}];
(我的路线守卫)
export class AdminRouteGuard implements CanActivate {
constructor(@Inject(DOCUMENT) private document: any, private _authService: AuthService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this._authService.isLoggedIn()) {
return true;
}
else {
return this._authService.login();
}}}