角度依赖注入为什么我的服务方法按此顺序执行?

问题描述

我的前端首先通过Okta进行身份验证。用户特定的ID存储在声明中,并用于将配置对象从Cosmos DB中拉出。配置对象包含一些值,这些值将传递到其他服务上的端点,以检索将显示在前端的某些客户端数据。

依赖性树如下:

  • ConfigService取决于。(OktaAuthServiceHttpClient
  • DataService取决于(ConfigServiceHttpClient

在我的服务中,我的构造函数看起来像这样

配置服务。

export class ConfigurationService {

  constructor(public oktaAuth: OktaAuthService,private http: HttpClient) {
    console.log("Constucting Configuration Service");
    this.RetrieveVariables() 
  }

  private async RetrieveVariables() {
    await this.oktaAuth.getUser()
    .then(c => {
      this.user = c;
      console.log(c);
      this.tenantId = this.user.TenantId;
      this.getTenantConfiguration()
        .subscribe(config => {
          this.tenantConfig = config;
          console.log(this.tenantConfig);
          this.clientDataServiceUrl = this.tenantConfig.clientDataServiceUrl;
          this.dataLakeKey = this.tenantConfig.dataLakeAccessKey;
          this.dataLakeAccount = this.tenantConfig.dataLakeAccountName;
          console.log("retreived configService.tenantConfig")
        });
    });
  }

  private getTenantConfiguration(): Observable<TenantConfiguration> {
    var url = `${this.configurationServiceUrl}/tenant/${this.tenantId}`;
    try{
      var res = this.http.get<TenantConfiguration>(url);   
    }catch(e) {
       console.log(e);
    }
    return res;
  }
}

DataService

export class ClientDataService {

  constructor(private http: HttpClient,private config: ConfigurationService) {
    console.log("Constucting Client Data Service");
    this.clientDataServiceUrl = this.config.clientDataServiceUrl;
    this.dataLakeKey = this.config.dataLakeKey;
    this.dataLakeAccount = this.config.dataLakeAccount;
  }
}

由于某些原因,日志如下所示:

Constructing Configuration Service
Constructing Client Data Service
retrieved configService.TenantConfig

当我期望它们看起来像这样:

Constructing Configuration Service
retrieved configService.TenantConfig
Constructing Client Data Service

为什么在配置服务的构造函数完成之前就开始构建客户端数据服务?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)