C
C
Coder3212017-09-09 18:52:10
JavaScript
Coder321, 2017-09-09 18:52:10

How to inherit a class with injections?

There is a base class into which the service is injected. When inheriting this class and calling the super method in the child, it swears that the base class constructor takes one argument, that is, a service injection. At the moment I did this:

constructor(
    protected service: MyService
  ) {
    super(service);
  }

Everything works, but it looks clumsy, and I don’t want to do injections in all child classes. How to be in such a situation?
Found another option
const injector = ReflectiveInjector.resolveAndCreate([MyService]);
    this.service= injector.get(MyService);

But this option constantly creates a new instance of MyService, which is also not very good?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
furrya_black, 2017-09-10
@furrya_black

Don't inherit a service, @injectable class, pass it as a dependency.
Google creating services through factories.
In providers, instead of a service class, pass an object with properties useFactory, provide and deps, to deps injectable classes, they are the same services that the service specified in provide
uses. Not at home right now, if it doesn’t work out, write, I’ll be at the computer in the evening, I’ll throw some code)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question