小编use*_*836的帖子

角度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);
    });
}
Run Code Online (Sandbox Code Playgroud)

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

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

javascript typescript angular

5
推荐指数
1
解决办法
499
查看次数

标签 统计

angular ×1

javascript ×1

typescript ×1