从RC5迁移Angular 2.2.0注入=>切换注射

时间:2016-11-15 21:01:59

标签: angular typescript service

自从我将代码从Angular 2 RC5迁移到2.2.0后,我遇到了一个惊人的问题。我将服务注入到其他服务中:

  constructor(_http:Http,
          _cachingServices:CachingServices,
          _globalServices:GlobalServices,
          _dataServices:DataServices,
          _notificationServices:NotificationServices)

这在RC5中运行良好,但现在,在2.2.0版本中,注射混合了!以下代码:

  // Constructor
  constructor(_http:Http,
          _cachingServices:CachingServices,
          _globalServices:GlobalServices,
          _dataServices:DataServices,
          _notificationServices:NotificationServices){

console.debug(this.className + " param globalServices  => ",_globalServices);
console.debug(this.className + " param cachingServices => ",_cachingServices);
console.debug(this.className + " param dataServices => ",_dataServices);
console.debug(this.className + " param notificationServices => ",_notificationServices);

 }

给我以下结果:

  TranslateServices param globalServices  =>  DataServices {connectionStrings: Object, mode: "dev", magicServices: MagicServices, apiURL: "http://127.0.0.1/api.php", baseURL: "http://localhost:3000"}
  TranslateServices param cachingServices =>  CachingServices {expiration: EventEmitter, cacheduration: 400000}
  TranslateServices param dataServices =>  NotificationServices {notification: EventEmitter, types: Object, notification_one_like_per_day: "notification_one_like_per_day", notification_one_message_per_day: "notification_one_message_per_day", notification_thanks_for_message: "notification_thanks_for_message"…}
  TranslateServices param notificationServices =>  GlobalServices {mode: "dev", apiURL: "http://127.0.0.1/api.php", baseURL: "http://localhost:3000"}

错误=>服务参考是混合的! 我已经搜索了一段时间而没有任何回答。

谢谢!

1 个答案:

答案 0 :(得分:0)

天啊!多么可疑的问题。我的解决方案有几项服务。其中每个服务都引用了另一项服务,但没有循环引用

我们采取两项服务: 的 translate.services.ts

  export class TranslateServices extends BaseServices {

  // Constructor
  constructor(_http:Http,
              _globalServices:GlobalServices,
              _cachingServices:CachingServices,
              _dataServices:DataServices,
              _notificationServices:NotificationServices,
              _ipServices:IPServices,
              _configurationServices:ConfigurationServices){
  }
}

user.services.ts

export class UserServices extends BaseServices {

  constructor(_http:Http,
              _globalServices:GlobalServices,
              _cachingServices:CachingServices,
              _dataServices:DataServices,
              _notificationServices:NotificationServices,
              _ipServices:IPServices,
              _cookieServices:CookieServices) {
  }
}

我们可以看到 translate.services 注入 configuration.services user.services 注入 cookie.services

在我的情况下,如果首先初始化translate.services,则执行以下命令:

    console.debug("TranslateServices _configurationServices => ",_configurationServices);
    console.debug("UserServices _cookieServices => ",_cookieServices);

将提供以下输出:

TranslateServices _configurationServices => ConfigurationServices
UserServices _cookieServices => ConfigurationServices

如果首先初始化user.services,则使用相同的命令:

    console.debug("TranslateServices _configurationServices => ",_configurationServices);
    console.debug("UserServices _cookieServices => ",_cookieServices);

将提供以下输出:

TranslateServices _configurationServices => CookieServices
UserServices _cookieServices => CookieServices

参数 _cookieServices _configurationServices 的位置似乎具有重要性,即使它们位于两个不同的服务中。

要解决这个问题,我必须在两个服务上给出相同的签名:

    export class TranslateServices extends BaseServices {

  constructor(_http:Http,
              _globalServices:GlobalServices,
              _cachingServices:CachingServices,
              _dataServices:DataServices,
              _notificationServices:NotificationServices,
              _ipServices:IPServices,
              _configurationServices:ConfigurationServices
              _cookieServices:CookieServices) {
  }
}

export class UserServices extends BaseServices {

  constructor(_http:Http,
              _globalServices:GlobalServices,
              _cachingServices:CachingServices,
              _dataServices:DataServices,
              _notificationServices:NotificationServices,
              _ipServices:IPServices,
              _configurationServices:ConfigurationServices
              _cookieServices:CookieServices) {
  }
}

这似乎不是一种正常行为。我努力寻找解释的开头。希望bug能在更新的版本中得到纠正。

如果有人可以给我更多解释,我想要。