如果使用Angular用户登录/注销,我如何在 中显示不同的数据? [关闭]

问题描述 投票:0回答:1

我正在构建一个使用会话cookie来跟踪用户是否已登录的基本应用程序。

如果用户注销,我想在其中显示一个“对不起,您看不到”组件,如果用户登录,则要显示一个“显示常规内容”组件。但是,只有一个标签,而且我无法弄清楚用户注销后如何更改该组件。

是否有办法做到这一点,或者我是否脱离基地?

angular router
1个回答
1
投票

我建议您仅使用一台路由器,并根据用户的身份更改该路由器所处的位置。

请参阅角度文档中的路径防护部分:

https://angular.io/guide/router#milestone-5-route-guards

例如,您可以设置一个受保护的路由“ admin”,并且仅在验证用户后才显示其子级。

const adminRoutes: Routes = [
  {
    path: 'admin',
    component: AdminComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: '',
        children: [
          { path: 'crises', component: ManageCrisesComponent },
          { path: 'heroes', component: ManageHeroesComponent },
          { path: '', component: AdminDashboardComponent }
        ],
      }
    ]
  }
];

非常重要。 “真实”身份验证需要在您的后端进行。自动重新路由角度是有帮助的,但是当应用程序在客户端运行时,可以更改为显示服务器返回的内容。

© www.soinside.com 2019 - 2024. All rights reserved.