【问题标题】:How to use CanActivate for child route correctly?如何正确使用 CanActivate 进行子路由?
【发布时间】:2018-03-17 07:25:58
【问题描述】:

我正在为我的路由器及其子路由器使用CanActivate 功能,但它不起作用 - 几个月前一直在使用相同的功能,但现在没有。

没有错误、警告或类似的东西我可以调试...应用程序刚刚运行,我可以像所有其他路由一样正常访问我想要保护的路由器。

你能看看下面的代码有什么问题吗? 问题是我什至没有得到任何错误。

作为信息,我正在使用 Angular 5。

app.router.ts

export const router: Routes = [

    { path: '', redirectTo: 'home', pathMatch: 'full'},
    { path: 'home', component: HomeComponent},
    { path: 'signup', component: SignupComponent},
    { path: 'dashboard', canActivate: [ AuthguardGuard ],
            children:
            [
                { path: '', loadChildren: './dashboard/dashboard.module#DashboardModule', pathMatch: 'full' }
            ]
    },

    { path: '**', redirectTo: 'page-not-found' }

];

export const appRoutes: ModuleWithProviders = RouterModule.forRoot(router);

dashboard.module.ts

const dashboardRoutes: Routes = [

    { path: 'user', redirectTo: 'user', pathMatch: 'full' },
    { path: 'user', component: UserComponent,
        children: [
            { path: '', component: EditComponent },
            { path: 'userMail', component: UserMailComponent },
            { path: 'userSettings', component: UserSettingsComponent}
        ]
    },
];

authguard.guard.ts

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { AuthService } from './_service/auth.service';


@Injectable()
export class AuthguardGuard implements CanActivate {
    constructor( private user: AuthService ) {
        console.log('In AuthGuard!');
    }
    canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        return this.user.isUserAuthenticated();

    }
}

auth.service.ts

import { Injectable } from '@angular/core';

@Injectable()
export class AuthService {

    public isUserAuthenticated;
    private userName;

    constructor() {
        this.isUserAuthenticated = false;
    }

    setUserLoggedIn() {
        this.isUserAuthenticated = true;
    }

    getUserLoggedIn() {
        return this.isUserAuthenticated;
    }

}

【问题讨论】:

  • 乍一看,您将 service 作为方法调用 return this.user.isUserAuthenticated() 并且您实际上有一个正在服务的属性。
  • 这意味着什么?几个月前使用相同的概念可以正常工作的正确方法是什么?
  • 我不知道这是否是问题所在,这只是我发现的第一个细节。我会尝试这样称呼:return this.user.isUserAuthenticated
  • 我会检查的。为什么我被否决了?这个问题有什么问题?
  • 不确定。不是我。

标签: angular canactivate


【解决方案1】:

问题已解决...我从 app.router.ts 中删除了这部分:

{path: '', loadChildren: './dashboard/dashboard.module#DashboardModule', pathMatch: 'full'}

并按如下方式使用:

export const router: Routes = [

    { path: '', redirectTo: 'home', pathMatch: 'full'},
    { path: 'home', component: HomeComponent},
    { path: 'signup', component: SignupComponent},
    { path: 'dashboard', canActivate: [ AuthguardGuard ],
        children:[
           { path: '', component: EditComponent },
           { path: 'userMail', component: UserMailComponent },
           { path: 'userSettings', component: UserSettingsComponent}
        ]
    },

    { path: '**', redirectTo: 'page-not-found' }
];
export const appRoutes: ModuleWithProviders = RouterModule.forRoot(router);

而且我可以访问 authguard.guard.ts 文件,我可以直接得到预期的结果。

【讨论】:

  • 如何从 AuthGuard 中获取特定的 childRoute,例如:userSettings。将其保留为子项,但无需 AuthGuard 即可访问。
  • @NajamUsSaqib,取决于您的整个概念。你能请。在此处发布您的一些路由器代码。将有助于理解概念。
  • 我有几乎相同的路由,并通过使用相同的父级进行 2 个路由解决了这个问题。示例:有 Guard { path: 'dashboard', canActivate: [ AuthguardGuard ], children:[ { path: '', component: EditComponent }, { path: 'userMail', component: UserMailComponent }, ] }, 没有 Guard 和 { path: 'dashboard', children:[ { path: 'userSettings', component: UserSettingsComponent} ] },
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多