First of all, I wanted to make clear that what I want is the Angular Type instance of a component, not the TypeScript definition of that component.
The context is the following:
I'm inside a service and I receive in input the instance of a component (HomeComponent
with all the component properties specified). I can also access the ViewContainerRef and the TemplateRef. However, I didn't find anything that I can pass to resolveComponentFactory to obtain a new instance of the object.
I tried:
component = *component instance*
componentFactoryResolver.resolveComponentFactory(component)
But it throws the following error:
No component factory found for [object Object]. Did you add it to @NgModule.entryComponents?
Obviously the component is correctly imported (I've an instance of it!).
I tried different combinations with the same result, but I still can't get the Type
instance that Angular wants.
I don't know a priori the list of possible components, so I can't use a map or import them in the service. How can I obtained the wanted instance?
I'm currently using Angular5, so I still need the ComponentFactoryResolver.
Through experimentation, I found out that the Angular
Type instance is actually a string representing the constructor of the component.
For this reason, it was as easy as:
componentFactoryResolver.resolveComponentFactory(component.constructor)