【发布时间】:2018-10-26 17:27:48
【问题描述】:
我正在尝试在我正在创建的动态组件中注入服务,但是当我尝试注入服务时出现错误。我能够将服务注入到使用 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);
});
}
在上面的代码中,我在动态模块中为提供者添加了申请者Svc,然后注入到动态组件构造函数中,但是每当我尝试这样做时,我都会遇到错误
ERROR 错误:无法解析 class_1 的所有参数:(?)。 ...... ..... 在 JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules (compiler.js:22570)
【问题讨论】:
-
您是否将ApplicantSvc 添加到您的模块中,例如app.module.js?
-
我已经在 app.module 中添加了它,它在 AOT 编译的其他组件中运行良好。只遇到 JIT 编译组件的问题。
标签: javascript angular typescript