Asked By: Anonymous
I’m creating a class that extend HttpClient
then extending this
keyword through includeEventsIn
function, includeEventsIn
will extend BaseService with EventAggregator type, see code here.
Question
- How can I include
EventAggregator
methods and props inBaseService
class along with HttpClient? - What would be another approach to extend class(BaseService) with two class?
Here’s the code.
@transient()
export class BaseService extends HttpClient {
constructor() {
super();
includeEventsIn(this);
}
}
Solution
Answered By: Anonymous
You shouldn’t use inheritance when building Aurelia apps unless absolutely necessary (and I would argue it’s never necessary). Favor composition over inheritance.
For example, in your case, I would have Service
(not BaseService
, because you would no longer be using inheritance) take a dependency on HttpClient
and EventAggregator
. I doubt you truly need all of the functionality that gets added by the call to includeEventsIn
. That being said, if you do need it, then you can simply add the three functions to Service
.
import {inject, EventAggregator} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
@inject(HttpClient, EventAggregator)
export class Service {
constructor( http, ea ) {
this.http = http;
this.ea = ea;
}
}
You can then inject this class in to another class as needed, after implementing whatever functionality it needs.