Ember-CLI cannot register service
I am trying to register a "session" service from a session initializer.
/services/session.js
/* Imports */
import Ember from 'ember';
/* Session */
var Service = Ember.Object.extend({
sayHi: function() {
console.log('Hello From Session Service');
}
});
/* Exports */
export default Service;
/initializers/session.js
/* Session Initializers */
var Initializer = {
name: 'Session',
initialize: function(Container, App) {
App.register('service:session', App.Session);
// This line doesn't work. See error below.
App.inject('route', 'Session', 'service:session');
// This works, I can access Session from my routes.
App.inject('controller', 'Session', 'service:session');
// This works, I can access Session from my
}
};
/* Export */
export default Initaializer;
Error: Unprepared TypeError: Attempting to register an unknown factory:service:session
Can I register a service?
+3
source to share
1 answer
You do not need to register a service, the recognizer will search for it for you. The id adjusts everything to look like this:
applications / services / session.js:
import Ember from 'ember';
export default Ember.Object.extend({
sayHi: function() {
console.log('Hello From Session Service');
}
});
app / initializers / session service.js:
export default {
name: 'session-service',
initialize: function(container, app) {
app.inject('route', 'session', 'service:session');
app.inject('controller', 'session', 'service:session');
}
};
This is almost what you get when you use it ember generate service session
.
+3
source to share