我想注入' eventBus'服务到我的组件但注入服务未定义。我正在使用ember 2,我正在学习本教程(https://guides.emberjs.com/v2.5.0/applications/services/),但它对我不起作用。
的服务/事件bus.js
import Ember from 'ember';
export default Ember.Service.extend(Ember.Evented, {
init() {
this._super(...arguments);
},
publish: function() {
return this.trigger.apply(this, arguments);
},
subscribe: function() {
this.on.apply(this, arguments);
},
unsubscribe: function() {
this.off.apply(this, arguments);
}
});
部件/间 - list.js
import Ember from 'ember';
const RoomList = Ember.Component.extend({
eventBus: Ember.inject.service(),
didReceiveAttrs () {
this.set('currentRoomId', this.get('params')[1].id);
},
model: Ember.computed('params.[]', function(){
return this.get('params')[0];
}),
actions: {
onRoomClicked(roomId) {
this.get('eventBus').publish(); //Uncaught TypeError: Cannot read property 'publish' of undefined
this.set('currentRoomId', roomId);
},
deleteRoom(room) {
console.log('delete room', room);
}
}
});
RoomList.reopenClass({
positionalParams: 'params'
});
export default RoomList;
内onRoomClicked
方法this.eventBus