Angular 2身份验证:注销保留在用户页面中

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

我的应用程序有两个功能:AppModule和UserModule

用户模块-用于登录用户

AppModule-登录页面

我在我的
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
中完成的路由处理将您路由到了错误的组件?