【发布时间】:2018-06-01 21:33:15
【问题描述】:
我已经设置了一个 AuthResolve 类,以确保在显示路由之前完成身份验证,但由于某种原因,解析器没有被调用。解析器函数和构造函数都不是。控制台不记录我插入的日志。我不明白这是怎么回事。
根级路由:
export const appRoutes: Routes = [
{
path: '',
component: CallbackComponent,
canActivate: [AuthGuardService],
pathMatch: 'full',
resolve: {
auth: AuthResolve,
},
},
{
path: 'applicant', component: ApplicantViewComponent,
canActivate: [AuthGuardService],
children: [...applicantRoutes],
resolve: {
agency: AgencyResolve,
mapping: SectionMappingResolve,
auth: AuthResolve,
},
},
{
path: 'agency', component: AgencyViewComponent,
canActivate: [AuthGuardService],
children: [...agencyRoutes],
resolve: {
agency: AgencyResolve,
mapping: SectionMappingResolve,
auth: AuthResolve,
},
},
{
path: 'tos', component: TosComponent,
canActivate: [AuthGuardService],
resolve: {
auth: AuthResolve,
},
},
{
path: 'eua', component: EuaComponent,
canActivate: [AuthGuardService],
resolve: {
auth: AuthResolve,
},
}
];
auth-resolve.ts:
@Injectable()
export class AuthResolve implements Resolve<User> {
constructor(private authService: AuthService) {
console.log('AuthResolve.constructor');
}
resolve(route: ActivatedRouteSnapshot): Observable<User> {
console.log('AuthResolve.resolve');
const authHandle = this.authService.handleAuthentication()
authHandle.subscribe(() => {
this.authService.scheduleRenewal();
});
return authHandle;
}
}
为什么我的解析器没有被调用?
【问题讨论】:
-
您是否将 AuthResolve 添加到您的 module.ts 中的提供程序??
-
它在我的应用模块中提供。否则认为我会得到一个错误。
-
你确定它会去那里吗,你可以尝试从路径
''中删除canActivate: [AuthGuardService],。 -
成功了,@AnshumanJaiswal。您可以将其添加为答案,我会接受。
-
@BBaysinger 将我的评论添加为答案,您可以接受。干杯!!