如果用戶沒有登錄,想要去一些安全的根,例如「/ items」authguard返回false並且不導航「/ login」。Angular2 Authguard登錄路徑
export const APP_ROUTES: Routes = [
{path: '', component: PublicComponent, children: PUBLIC_ROUTES},
{
path: '',
component: SecureComponent,
resolve: {user: UserResolver},
canActivate: [AuthGuard],
children: SECURE_ROUTES
},
{path: '', redirectTo: '/login', pathMatch: 'full'}
];
AuthGuard代碼:
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private userService: UserService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
let url: string = state.url;
return this.checkLogin(url);
}
checkLogin(url: string): Observable<boolean> {
this.userService.redirectUrl = url;
return this.userService.getPrincipal()
.map(user => {
if(user)
return true;
return false;
});
}
}
問題是什麼在這裏:)? –
如果用戶沒有登錄,那麼我想導航登錄頁面? :) – vangoo
@vangoo您是否嘗試過我的代碼? –