【问题标题】:Angular how to disable authGuard on particular child pageAngular 如何在特定子页面上禁用 authGuard
【发布时间】:2021-08-12 12:14:23
【问题描述】:

任何人都可以建议在单个子页面上禁用 authguard(应用于父级)的最佳方法。

ROUTES:Routes = [
    {
        path: '',
        component: AdminComponent,
        children: [
            {
                path: '',
                pathMatch: 'full',
                data: { title: 'Home' },
                loadChildren: 'src/app/admin/dashboard/modules/dashboardRouteModule#DashboardRouteModule',
            },
            {
                path: 'dashboard',
                redirectTo: ''
            },
            {
            path : 'blog',
            data: { title: 'Blog', breadcrumb: 'Blog' },
            loadChildren: 'src/app/admin/blog/modules/blogRouteModule#BlogRouteModule'
            },
             {
                path: '**',
                component: NotFoundComponent
            }],
    canActivate: [ AuthGuard ]}];

博客路由模块

ROUTES:Routes = [
    {
        path: '',
        component: BlogComponent,
        resolve: {
            routeValues: BlogCreateResolve
        },
    },
    {
        path: ':id',
        resolve: {
            routeValues: BlogEditResolve
        },
        runGuardsAndResolvers: 'always',
        children: [
            {
                path: '',
                component: BlogEditComponent,
                canDeactivate: [CanDeactivateGuard],
            },
            {
                path: 'list',
                data: { title: 'BlogList', breadcrumb: 'List' },
                component: BlogListComponent,
                resolve: {
                    list: BlogListResolve
                }
            }]}]

目前,所有页面都受到限制,需要登录,但我想禁用 'list' 路由的 authguard,以便无需登录即可查看页面。

谢谢

【问题讨论】:

  • 我之前想出并实施的一种方法是禁用父级中的防护并将其设置在每个子级上。
  • 这将是相当困难的,因为有相当多的模块和路线。

标签: angular angular-routing angular-router angular-router-guards auth-guard


【解决方案1】:

也许你可以在你的守卫中添加一个条件,当 URL 与你想要删除的组件中的 URL 匹配时允许导航。

【讨论】:

  • 已经走上了这条路,但我只是觉得这不是好方法。如果有一种类似于 C# 中 Allowannonymous 的简洁方法,我会很好。
猜你喜欢
  • 1970-01-01
  • 2011-10-10
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2018-07-26
相关资源
最近更新 更多