使用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。为什么需要这个以及它们各自是什么?这种方式很混乱。
答案 0 :(得分:3)
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 Data会做这样的事情,所以你可以使用:
this.get('store');
通过您的服务,您现在可以:
this.get('geoService');