为什么 Ember 初始值设定项有 3 个不同的名称

why are there 3 different names for an Ember initializer

当您使用 Ember CLI 生成服务时,您会得到如下内容:

export function initialize(container, application) {
  application.inject('route', 'geoService', 'service:geo');
  application.inject('component', 'geoService', 'service:geo');
}

export default {
  name: 'geo-service',
  initialize: initialize
};

您会注意到该服务包含 3 种不同的名称格式 - geoService、service:geo 和 geo-service。为什么需要这个,每个是什么?这种方式很混乱。

export function initialize(container, application) {
  /* service:geo is the factory name for a GeoService object
     the container keeps track of all the factories using this format.
  */

  // inject to every route the GeoService and
  // make it available through the property geoService
  application.inject('route', 'geoService', 'service:geo');

  // inject to every component the GeoService and
  // make it available through the property geoService
  application.inject('component', 'geoService', 'service:geo');
}

export default {
  /*
    The name of your service is geo, but by convention
    all objects are suffixed with the object type name,
    ex:
    ClientsView
    ClientsRoute
    ClientsController
    GeoService

    In Ember CLI The dashed format is the convention
    for file names and names in general.
  */
  name: 'geo-service',
  initialize: initialize
};

Ember 数据做这样的事情,所以你可以使用:

this.get('store');

通过您的服务,您现在可以做到:

this.get('geoService');