我只想使用Aurelia Event聚合器发布和订阅一个简单的事件。它甚至没有在第一次加载时发布,我需要去class2几次,然后在开始发布一段时间后再发布。我不知道这是什么问题。谢谢大家的帮助
import { autoinject } from "aurelia-framework";
import { Router } from "aurelia-router";
import { EventAggregator } from "aurelia-event-aggregator";
import { TestEvent } from 'shared/TestEvent';
@autoinject
export class Class1{
constructor(private router: Router, private ea: EventAggregator){
this.ea = ea;
}
try(){
this.ea.publish(new TestEvent);
}
}
import { autoinject, observable } from "aurelia-framework";
import { Router } from "aurelia-router";
import { EventAggregator } from 'aurelia-event-aggregator';
import { TestEvent } from 'shared/TestEvent';
@autoinject
export class Class2{
subscription: any;
constructor(private router: Router, private ea: EventAggregator) {
}
attached() {
this.subscription = this.ea.subscribe(TestEvent, ()=>{
console.log("message")
})
}
detached(){
this.subscription.dispose();
}
TestEvent.cs
export class TestEvent{}