Angular : 使用来自特定服务的方法作为模块的 useFactory 提供者

Angular : use a method from a specific service as useFactory provider for a module

我的 Angular5 应用程序中有一个模块,我是这样使用它的:

import *....
@NgModule({

  providers: [
    AuthentificationService,
    {
      provide: AuthHttp,
      useFactory: AuthentificationService.MYMETHOD,
      deps: [Http, RequestOptions, EnvVarsService, LocalStorageService, RouteNavigator, ReloadTokenEventService]
    }
  ]
})

export class AuthModule {
  constructor( ) {}

}

我的问题是我想使用自定义方法:我在 AuthentificationService

中定义的 MYMETHOD

我的服务是这样的:

@Injectable()
export class AuthentificationService {

  constructor() {}

  public authHttpServiceFactory(http: Http, options: RequestOptions,
                                envVarsService: EnvVarsService,
                                localStorageService: LocalStorageService,
                                router: RouteNavigator,
                                reloadTokenEventService: ReloadTokenEventService) {

    return new AuthHttp(new AuthConfig({
      tokenName: 'X-Auth-Token',
      headerName: 'X-Auth-Token',
      noTokenScheme: true,
      noJwtError: true,
      tokenGetter: (() => this.getAccessToken(http, options, envVarsService, localStorageService, router, reloadTokenEventService)),
      globalHeaders: [{'Content-Type': 'application/json'}],
    }), http, options);
  }


  private getAccessToken(): Promise<string> {
         // SOME TREATMENT
    }
  }

}

可是我好像找不到(AuthentificationService.MYMETHOD)

有什么建议吗?

试试这个,它应该可以使用导出函数,将 AuthentificationService 添加到 deps 数组

export function myFactory(authService: AuthentificationService) {
     return () => authService.yourMethod();
}

providers: [
AuthentificationService,
{
  provide: AuthHttp,
  useFactory: myFactory,
  deps: [AuthentificationService, Http, RequestOptions, EnvVarsService, LocalStorageService, RouteNavigator, ReloadTokenEventService],
  multi: true
}
]