【问题标题】:Angular 2+ route resolvers not being called未调用 Angular 2+ 路由解析器
【发布时间】: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 将我的评论添加为答案,您可以接受。干杯!!

标签: angular angular-router


【解决方案1】:

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

canActivate: [AuthGuardService]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 2017-01-14
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多