Aurelia Event Aggregator not working

Aurelia event aggregator is having big problems in my applicaton.
import { ApiService } from ‘./…/…/shared/ApiService’;
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 eventAggregator: EventAggregator, private api: ApiService) {
    this.eventAggregator = eventAggregator;
  }
  attached() {
  }

  try() {
    console.log('asd')
    this.eventAggregator.publish("test");
  }
  navigateToStep2() {
    this.router.navigate('listyourcar-3')
  }
}

The .ts file i want to subscribe in is
import { ApiService } from ‘./…/…/shared/ApiService’;
import { autoinject } from “aurelia-framework”;
import { Router } from “aurelia-router”;
import { DialogService } from “aurelia-dialog”;
import { EventAggregator } from ‘aurelia-event-aggregator’;
import { TestEvent } from ‘shared/TestEvent’;

@autoinject
export class Class2{

  listOfImages: any;
  imageId: any;
  subs: any;
  constructor(private router: Router, private dialog: DialogService, private api: ApiService,
    private eventAggregator: EventAggregator) {

    this.eventAggregator = eventAggregator;

    this.listOfImages = [];
  }

  attached(){
    this.subs = this.eventAggregator.subscribe("test", () => {
      console.log('subscribed')
    })
  }
  detached(){
    this.subs.dispose();
  }

If i visit the class2 component than on the second time the method subscribes but when not added dispose. When i add dispose(); the method dont subscribes any time.
Can you help

Can you help describe the issue clearer? Based on what you described, I made this as a base for you to repro https://codesandbox.io/s/v64x168yl0

1 Like