#How to inject instance of the component using DI?

3 messages · Page 1 of 1 (latest)

jade robin
#

is it possible to get the instance of the component without using this? something like this?

@Component(...)
export class ... {
  n = (() => {
    const componentRef = inject(???);
    componentRef === this
  })();
}

I try to create a inject function and i need the instance of the component and i don't want to pass this into it just to get it in the DI.
also I don't want to pass a type of component because it should be generic.

twin orbit
#

As far as I know there is no generic token for "the component instance that this node injector belongs to".
What's your use case? There may be other ways to achieve this.
Otherwise you'll have to create an InjectionToken and provide it in the component.

spiral cosmos
#

And even if you do that, you'll get an error if you try to get it using inject at construction time since you would need the component to construct the component, leading to a circular dependency problem. What's wrong with this?