Unable to Inject angular services in to dynamically loaded component

喜欢而已 提交于 2019-12-12 04:54:22

问题


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?

来源:https://stackoverflow.com/questions/45424018/unable-to-inject-angular-services-in-to-dynamically-loaded-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!