停用ngx-rocket angular 7入门套件中某些页面的登录屏幕

时间:2018-11-04 23:30:11

标签: angular angular7 canactivate

我正在使用ngx-rocket angular7入门套件。它实现了一个保护措施,如果我尚未登录,将使登录屏幕弹出。为了实现注册页面,我不希望弹出登录屏幕,注册页面应该是公开的。有谁知道如何停用那里的登录屏幕?

1 个答案:

答案 0 :(得分:1)

这取决于您如何实现注册页面,显示代码,在何处定义注册链接和路由。通常,该模板在辅助函数中使用AuthenticationGuard。检查About模块:

const routes: Routes = [
  Shell.childRoutes([
    { path: 'about', component: AboutComponent, data: { title: extract('About') } }
  ])
];

如果使用不带Shell.childRoutes辅助函数的路由定义注册模块,则它将不会触发AuthenticationGuard,因此不会重定向到login。像这样:

const routes: Routes = [
    { path: 'register', component: RegisterComponent, data: { title: extract('Register') } }
];

如果还想保留原始的Shell功能,则路由可以如下:

const routes: Routes = [
   {
      path: '',
      component: ShellComponent,
      children: [
        { path: 'register', component: RegisterComponent, data: { title: extract('Register') } }
      ],
      // canActivate: [AuthenticationGuard],
      // Reuse ShellComponent instance when navigating between child views
      data: { reuse: true }
    }

];

作为替代方案,可以在Shell中自定义帮助程序:

export class Shell {

  // existing helper
  static childRoutes(routes: Routes): Route {
    return {
      path: '',
      component: ShellComponent,
      children: routes,
      canActivate: [AuthenticationGuard],
      // Reuse ShellComponent instance when navigating between child views
      data: { reuse: true }
    };
  }

  // add new helper without guard
  static unAuthenticatedChildRoutes(routes: Routes): Route {
    return {
      path: '',
      component: ShellComponent,
      children: routes,
      // Reuse ShellComponent instance when navigating between child views
      data: { reuse: true }
    };
  }
}

然后,在您的组件中:

const routes: Routes = [
  Shell.unAuthenticatedChildRoutes([
    { path: 'register', component: RegisterComponent, data: { title: extract('Register') } }
  ])
];