Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReflectiveInjector unable to resolve parameters of parent class #45

Closed
paulhobbel opened this issue May 6, 2020 · 2 comments
Closed

Comments

@paulhobbel
Copy link

Seems like the ReflectiveInjector is unable to resolve parameters of the parent class.
Example:

class SomeService {
  fetch() {
    console.log('fetching...');
  }
}

class abstract Parent {
  constructor(private service: SomeService) {}

  protected fetchAndDoStuff() {
    this.service.fetch();
  }
}

class Child extends Parent {
   
}

The injector should resolve the Parent constructor as no constructor is defined in Child. Instead it does nothing.

@mgechev
Copy link
Owner

mgechev commented May 6, 2020

That's the right way to go:

import { Injectable, ReflectiveInjector } from 'injection-js';
import 'reflect-metadata';

class SomeService {
  fetch() {
    console.log('fetching...');
  }
}

abstract class Parent {
  constructor(private service: SomeService) {}

  protected fetchAndDoStuff() {
    this.service.fetch();
  }
}

@Injectable()
class Child extends Parent {
  constructor(service: SomeService) {
    super(service);
  }
}

const injector = ReflectiveInjector.resolveAndCreate([
  {
    provide: SomeService,
    useClass: SomeService
  },
  {
    useClass: Child,
    provide: Child
  }
])

console.log(injector.get(Child));

There seems to be something off with your Parent class declaration. The abstract keyword should be before class. You're also missing Injectable() decorators.

@mgechev mgechev closed this as completed May 6, 2020
@paulhobbel
Copy link
Author

paulhobbel commented May 7, 2020

Oh sorry that was just a markup issue of me. Even then it fails to resolve. I mean it works if you add the constructor again, but it should work without the constructor as it should resolve the parent's constructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants