Angular 2+路线解析器未被调用

时间:2018-06-01 21:33:15

标签: angular angular-router

我已经设置了一个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;

  }
}

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

1 个答案:

答案 0 :(得分:1)

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

canActivate: [AuthGuardService]