本文介绍了Angular 2+ 路由解析器不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个 AuthResolve 类以确保在显示路由之前完成身份验证,但是由于某种原因,解析器没有被调用.既不是解析器函数也不是构造函数.控制台不会记录我插入的日志.我不明白这是怎么回事.

I have set up an AuthResolve class to ensure authentication is complete before displaying a route, but for some reason, the resolver is not getting called. neither the resolver function nor the constructor. The console does not log the logs I have inserted. I do not understand how this can be.

根级路由:

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:

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;

  }
}

为什么我的解析器没有被调用?

Why are my resolvers not getting called?

推荐答案

从您的第一条路线中删除以下内容:

Remove following from your first route:

canActivate: [AuthGuardService]

这篇关于Angular 2+ 路由解析器不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:25