app.js
import {inject} from 'aurelia-framework';
import {EventAggregator} from 'aurelia-event-aggregator';
@inject(EventAggregator)
export class App {
constructor(eventAggregator) {
this.eventAggregator = eventAggregator;
}
publish() {
var payload = 'This is some data...';
this.eventAggregator.publish('myEventName', payload);
}
subscribe() {
this.subscriber = this.eventAggregator.subscribe('myEventName', payload => {
console.log(payload);
});
}
dispose() {
this.subscriber.dispose();
console.log('Disposed!!!');
}
}
我们需要点击SUBSCRIBE按钮以侦听将来发布的数据。连接订阅者后,无论何时发送新数据,控制台都会记录它。如果我们点击PUBLISH按钮五次,我们会看到它每次都被记录下来。
我们还可以通过单击DISPOSE按钮。