简体   繁体   English

Angular2 - 当注入循环引用的动态组件时,依赖注入不起作用

[英]Angular2 - Dependency injection not working when injected into a dynamic component of cyclic reference

I have a situation where based on the input selected (radio or checkbox), I add a component dynamically through ajax. 我有一种情况,根据所选的输入(无线电或复选框),我通过ajax动态添加一个组件。

Plunkr: https://plnkr.co/edit/uvGo3RxCkUPeuknVu9m8?p=preview Plunkr: https ://plnkr.co/edit/uvGo3RxCkUPeuknVu9m8 p = preview

I explain the flow for better understanding: 我解释了流程以便更好地理解:

First Layer: src/parent/parent.ts 第一层: src/parent/parent.ts

We get the json response from questions.json and populate the questions. 我们从questions.json获取json响应并填充问题。

Based on the controlType we load either a-component or b-component in src/parent/parent.html . 基于controlType我们在src/parent/parent.html加载a-componentb-component

When I select a checkbox in a-component or a radio button in b-component , I call createDynamicComponent() and pass in the componentData . 当我在a-component选择一个复选框或在b-component选择一个单选按钮时,我调用createDynamicComponent()并传入componentData

componentData is nothing but the metadata information on the next component that needs to be populated, based on the selected option. componentData只是基于所选选项需要填充的下一个组件的元数据信息。

Now, the componentData is passed into the dynamic-component from src/controls/a.html or src/controls/b.html 现在, componentData被传递到dynamic-componentsrc/controls/a.htmlsrc/controls/b.html

Inside dynamic-component , I resolve the provider and inject the questions data into the entryComponents, ( a-component and b-component ) dynamic-component内部,我解析提供程序并将questions数据注入entryComponents( a-componentb-component

Everything was working fine, until I introduced a HService (which holds the component data building logic) inside both a-component and b-component . 一切都运行正常,直到我在a-componentb-component引入了HService (它包含组件数据构建逻辑)。

I am getting the following error, Error: Can't resolve all parameters for BComponent: ([object Object], [object Object], ?) 我收到以下错误, Error: Can't resolve all parameters for BComponent: ([object Object], [object Object], ?)

The last parameter being the HService inside the constructor of b-component , If I take out the service and use the commented code, everything works fine. 最后一个参数是b-component构造函数中的HService ,如果我取出服务并使用注释代码,一切正常。

 this.componentData = { 'questions': { "question": "This is a " + this.level + " level question?", "controlType": "radio", "answers": [ "Yes", "No", "None" ] }, "component": BComponent, "level": this.level }; 

Edit 1: I have the HService injected into a-component already and there is no provider resolve error and it is working fine. 编辑1:我已经将HService注入到a-component并且没有提供程序解析错误,并且它正常工作。 Only on adding it to b-component , I face the error. 只有在将其添加到b-component ,我才会面临错误。

Help will be much appreciated!! 帮助将不胜感激!!

I think the problem is based on circular dependencies. 我认为问题是基于循环依赖。

A <=> HService and B <=> HService A <=> HServiceB <=> HService

To solve it i would unleash dependencies by creating abstract class for HService and use it as token: 为了解决这个问题,我将通过为HService创建抽象类来释放依赖关系,并将其用作标记:

base.h.ts base.h.ts

export abstract class BaseHService {
    private componentData: any;

    abstract getComponentData(queries: IQ[], level: number): any;
}

app.module.ts app.module.ts

providers: [ 
  { provide: BaseHService, useClass: HService }, <== this line
  { provide: 'questions', useValue: {} }, 
  { provide: 'level', useValue: 1 }
],

a.ts a.ts

import { BaseHService } from "../h/base.h";
...
constructor(...private _h: BaseHService) {

b.ts b.ts

import { BaseHService } from "../h/base.h";
...
constructor(...private _h: BaseHService) {

Modified Plunker 改良的Plunker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM