问题
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