@NgModule loading dynamic components on runtime using SystemJs

懵懂的女人 提交于 2019-12-11 18:41:57

问题


I am trying to load dynamic components on run time using the compileModuleAndAllComponentsAsync function. while loading the components i am trying to inject a service called ServicetestService.

Now what is happening is that when the code reaches :

Heading ##const cmp = factory.create(inj, [], null, moduleRef);

I am getting this error:

"Error: StaticInjectorError(ServicetestService)[AnimationBuilder -> RendererFactory2]: \n NullInjectorError: No provider for RendererFactory2!\n at NullInjector.get

Here is the code i am using:

return SystemJS.import(`${url}`).then((module) => {
        return this.compiler.compileModuleAndAllComponentsAsync(module[`${moduleInfo.moduleName}`]).then(moduleWithCompFactories => {              
            const inj = Injector.create(
                {
                    providers: [
                        {
                            provide: ServicetestService, 
                            deps: []
                        }
                    ],  
                    //parent: null,  
                    name: 'ServicetestService' 
                });
            const moduleRef = moduleWithCompFactories.ngModuleFactory.create(inj);
            if(moduleInfo.moduleName === 'LibrarytestModule'){

                //return moduleRef;
                console.log("module:");
                console.log(module);

                console.log("moduleref");
                console.log(moduleRef);

                console.log("factory");
                const factory = moduleWithCompFactories.componentFactories[1];
                console.log(factory);

                console.log("cmp");
                const cmp = factory.create(inj, [], null, moduleRef);
                cmp.instance.ServicetestService = ServicetestService;

                console.log(cmp);
            }
            return module;;
        });
    });

this is the syntax of the service i am trying to inject:

import { AbstractServicetestService } from 'abstractclasses';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})

export class ServicetestService extends AbstractServicetestService{

constructor() {
   super();
}

printhello(){
   alert("Hello from service elie");
}

来源:https://stackoverflow.com/questions/58709713/ngmodule-loading-dynamic-components-on-runtime-using-systemjs

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