当我刷新页面时,我返回主页

时间:2019-10-01 10:23:52

标签: angular

在“用户列表”页面上时,我遇到了这样的问题,刷新后,我将其带到主页。该页面在前端以斜角书写,在后端以.net书写。 我的app.routing模块如下所示:


const routes: Routes = [
  {
    path: '',
    loadChildren: './modules/dashboard/dashboard.module#DashboardModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'users',
    loadChildren: './modules/user/user.module#UserModule',
    canActivate: [AuthGuard]
  },
  {
    path: 'settings',
    loadChildren: './modules/settings/settings.module#SettingsModule',
    canActivate: [AuthGuard]
  },
...
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, useHash: true })],
  exports: [RouterModule]
})
export class AppRoutingModule { }

auto.guard.ts

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(
    private router: Router,
    private store: Store<AuthState>
  ) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.store.pipe(
      select(loggedIn),
      map((isLoggedIn) => {
        if (!isLoggedIn) {
          this.store.dispatch(logout());
          this.router.navigate([RouterPath.Login]);
        }

        return !!isLoggedIn;
      })
    );
  }
}

0 个答案:

没有答案