提问者:小点点

Angular 6:无法使用JIT编译在动态组件中注入服务


我正在尝试在我正在创建的动态组件中注入服务,但是当我尝试注入服务时,我收到了一个错误。我能够将服务注入使用AOT的所有其他组件中,但当我使用JIT时却不能。这是代码。

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

@Injectable
export class ApplicantSvc
{
 name:string;  
}

private createComponentFromRaw(template: string){

  const tmpCmp = Component({ template })(class {

    constructor(private app :ApplicantSvc){}

  });

  // Now, also create a dynamic module.
  const tmpModule = NgModule({
    declarations: [tmpCmp],
    imports: [CommonModule],
    providers: [ApplicantSvc],

  })(class {});


  this.compiler.compileModuleAndAllComponentsAsync(tmpModule)
    .then((factories) => {
      const f = factories.componentFactories[0];
      this.cmpRef = f.create(this.injector, [], null, this.moduleRef);
      this.cmpRef.instance.name = 'my-dynamic-component';
      this.vc.insert(this.cmpRef.hostView);
    });
}

在上面的代码中,我为动态模块中的提供者添加了ApplicantSvc,然后注入了动态组件构造函数,但每当我尝试这样做时,都会出现错误

错误错误:无法解析class_1的所有参数:(?)。…………在JitCompiller. push…/node_modules/@角/编译器/fem5/编译器.js.JitCompile._loadModules(编译器.js:22570)


共0个答案