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

use*_*836 5 javascript typescript angular

我试图在要创建的动态组件中注入服务,但是当我尝试注入服务时出现错误。我可以在使用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);
    });
}
Run Code Online (Sandbox Code Playgroud)

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

错误错误:无法解析class_1的所有参数:(?)。..... ..... at JitCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules(compiler.js:22570)

jgr*_*ten 0

我发现简单地像变量一样传递服务就可以了

\n\n
  this.compiler.compileModuleAndAllComponentsAsync(tmpModule)\n    .then((factories) => {\n      const f = factories.componentFactories[0];\n      this.cmpRef = f.create(this.injector, [], null, this.moduleRef);\n      this.cmpRef.instance.name = 'my-dynamic-component';\n      \xcc\xb6t\xcc\xb6h\xcc\xb6i\xcc\xb6s\xcc\xb6.\xcc\xb6v\xcc\xb6c\xcc\xb6.\xcc\xb6i\xcc\xb6n\xcc\xb6s\xcc\xb6e\xcc\xb6r\xcc\xb6t\xcc\xb6(\xcc\xb6t\xcc\xb6h\xcc\xb6i\xcc\xb6s\xcc\xb6.\xcc\xb6c\xcc\xb6m\xcc\xb6p\xcc\xb6R\xcc\xb6e\xcc\xb6f\xcc\xb6.\xcc\xb6h\xcc\xb6o\xcc\xb6s\xcc\xb6t\xcc\xb6V\xcc\xb6i\xcc\xb6e\xcc\xb6w\xcc\xb6)\xcc\xb6;\xcc\xb6\n      ... = .        \n});\n
Run Code Online (Sandbox Code Playgroud)\n