Unable to Inject angular services in to dynamicall

2019-09-14 20:17发布

问题:

I have successfully loaded a component dynamically using ComponentFactoryResolver and everything is working as I expected. But my problem is , I have one service named ComponentStoreFactory which I have already injected in other components in my application and it's working fine but when I inject this service in to Dynamically Loaded Component I am getting the below error.

Uncaught Error: Can't resolve all parameters for the MyDynamicComponent

Service(it's available in an independent module which already imported in app module)

 @Injectable()
    export class ComponentStoreFactory {
      getComponent(componentName: string): any {
        if (componentName === (RegisteredComponents.SomeComponent as string)) {
          return SomeComponent;
        }

      }
    }

Dynamic Component (When I add the second parameter in the constructor to inject service I am getting the error)

export class MyDynamicComponent implements AfterViewInit {


  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,private componentStoreFactory : ComponentStoreFactory ) {
  }

Module where the Dynamic Component used

@NgModule({
    declarations: [
        MyDynamicComponent
    ],
    imports: [
        CommonModule
    ],
    exports: [MyDynamicComponent],
    providers:[
        ComponentStoreFactory 
    ]
})
export class TestModule {

}

How can I resolve this issue?