0

I use the EventAggregator in several places in my app and everything works fine. However I’m having trouble injecting it into one of my classes. When the class constructor is called, the EventAggregator argument is undefined.

import {EventAggregator} from 'aurelia-event-aggregator';
import {inject} from 'aurelia-framework';

@inject(EventAggregator)
export class Test {
    constructor(eventAggregator) {
        debugger;
        this.eventAggregator = eventAggregator;
    }
}

When stopping at the debugger; line, the constructor’s eventAggregator argument is undefined.

This looks just like what I have done to use EventAggregator in many other classes, so what could be the issue?

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
Jeff G
  • 1,996
  • 1
  • 13
  • 22
  • are you using jspm- if so, what does `jspm inspect --forks` return? What is the value of `EventAggregator` at your breakpoint? – Jeremy Danyow Aug 18 '16 at 20:09
  • @JeremyDanyow It returns: Installed Forks npm:aurelia-event-aggregator 1.0.0-beta.1.2.0 1.0.0-beta.1.2.1 npm:aurelia-logging 1.0.0-beta.1.2.0 1.0.0-beta.1.2.1 – Jeff G Aug 18 '16 at 21:30
  • @JeremyDanyow The value of EventAggregator is function EventAggregator() {....} but the eventAggregator argument is undefined. – Jeff G Aug 18 '16 at 21:36

1 Answers1

1

I think this has to do with forks (multiple version of the same package installed).

  1. To clean this up, open your config.js file and delete everything inside the map: { node. For example- all this.
  2. Then execute jspm install.
  3. Then check for forks again. jspm inspect --forks
  4. If you still have forks, execute jspm install aurelia-_______ for each aurelia item listed in your package.json

In fact- you may want to skip right to step 4- this will ensure you get the latest, which is now 1.0.0 (RTM) version of aurelia.

Jeremy Danyow
  • 26,470
  • 12
  • 87
  • 133
  • 1
    Went ahead and upgraded to Aurelia 1.0, but that didn't fix the problem. It turns out that I was calling new() on the class to which I had just added the injection of EventAggregator. So when the class's constructor was called, the eventAggregator parameter wasn't passed, so it was undefined of course. – Jeff G Aug 19 '16 at 22:24
  • Ah- that would do it! At least you're upgraded and the forks are cleaned up – Jeremy Danyow Aug 19 '16 at 22:25
  • Now the question is, how do you use dependency injection in a class which is not a singleton since consumers won't have access to the injected classes when they call new()? Use default arguments? – Jeff G Aug 19 '16 at 22:28
  • @inject(Factory.of(MyClass)) http://stackoverflow.com/a/39040268/725866 – Jeremy Danyow Aug 19 '16 at 22:31
  • In the example, how is MyModel used within the App class? Normally it would be passed into the constructor and you can hold onto it with this.myModel = myModel and then use it as this.myModel. Plus, I don't get how the two classes in the example interact. Sorry I had to post here instead of on the other post but I don't have enough reputation points to do so. – Jeff G Aug 20 '16 at 00:10
  • Okay. I think I was looking at it in reverse. It appears that the **consumer** of the class (App in this case) needs to use Factory.of. Is that correct? – Jeff G Aug 20 '16 at 00:23
  • Alright. Now I've got it. Factory.of gives you access to the constructor with the injection arguments already set so they don't need to be passed by consumers. I have this all working now in my project. Thanks Jeremy! – Jeff G Aug 20 '16 at 00:40