Angular 2身份验证:注销保留在用户页面中
我的应用程序有两个功能:AppModule和UserModule 用户模块-用于登录用户 AppModule-登录页面 我在我的Angular 2身份验证:注销保留在用户页面中,angular,angular2-routing,Angular,Angular2 Routing,我的应用程序有两个功能:AppModule和UserModule 用户模块-用于登录用户 AppModule-登录页面 我在我的auth.service中管理我的auth,我还有一个警卫作为auth.guard.service 当我调用auth.service.logOut()时,用户已注销,但仍停留在面板中,路由未更改。 我还尝试添加this.router.navigate([''])但用户停留在/面板路径中 这是我的注销方法: logout() { console.log(&qu
auth.service
中管理我的auth,我还有一个警卫作为auth.guard.service
当我调用auth.service.logOut()
时,用户已注销,但仍停留在面板中,路由未更改。
我还尝试添加this.router.navigate([''])代码>但用户停留在/面板
路径中
这是我的注销方法:
logout() {
console.log("logging out..");
this.auth.logout();
this.userData = null;
this.isLoggedIn = false;
this.router.navigate(['']);
}
我在面板组件中调用此方法。问题在于用户停留在PanelComponent中,而不是“踢出”到登录页面,即HomeComponent
app.routing.module.ts
@NgModule({
imports: [
RouterModule.forRoot([
{ path: '', component: HomeComponent },
])
],
exports: [
RouterModule
]
})
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'panel',
component: PanelComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
children: [
{ path: 'userHome', component: userHomeComponent },
{ path: '', component: PanelHomeComponent }
]
}
]
}
])
],
用户面板.routing.module.ts
@NgModule({
imports: [
RouterModule.forRoot([
{ path: '', component: HomeComponent },
])
],
exports: [
RouterModule
]
})
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'panel',
component: PanelComponent,
canActivate: [AuthGuard],
children: [
{
path: '',
children: [
{ path: 'userHome', component: userHomeComponent },
{ path: '', component: PanelHomeComponent }
]
}
]
}
])
],
当我刷新页面时,我被转移回登录。为什么不刷新它就不能工作
更新:我的主页将我重定向回面板,尽管isLoggedIn
变量已更改。
我的HomeComponent正在将我重定向回面板,尽管它不应该这样做
家庭组件:
ngOnInit() {
//If he is alredy logged, redirect him to the panel
this.authService.login().subscribe(() => {
this.loaded = true;
if (this.authService.isLoggedIn) {
console.log("Navigated back to worker panel!");
this.router.navigate(['/worker-panel']);
}
});
在logOut()方法中,我将isLoggedIn
变量设置为false
。尽管如此,我的控制台输出导航回工作面板代码>如下更改您的父路由器,并使用this.router.navigate(['home'])退出功能中的代码>
RouterModule.forRoot([
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{ path: 'home', component: HomeComponent },
])
请添加路由器配置。嘿@GünterZöchbauer我添加了我的面板模块路由器。我将AuthGuard放在两个模块(功能)中,当您调用auth.service.logOut()
时,哪个路由是活动的。预期的行为是什么?请同时添加父路由器配置。我在面板组件中调用此方法。问题是用户停留在PanelComponent中,而不是“踢出”到登录页面,即HomeComponent
。看起来在HomeComponent
中完成的路由处理将您路由到了错误的组件?