简体   繁体   English

无法到达父组件中的@ViewChildren

[英]Can't get to @ViewChildren in parent component

I am trying to get control over the children instances of a component and can't go past an error. 我试图控制一个组件的子实例,并且不能出错。 I am trying to follow along the answers from this issue . 我正在努力遵循这个问题的答案。

The parent component Sequence contains children components of SequenceStep . 父组件Sequence中包含的子组件SequenceStep The parent contains the following declaration: 父级包含以下声明:

@ViewChildren(SequenceStep) steps: QueryList<SequenceStep>;

then I am trying to do something about the children: 然后我想对孩子们做些事情:

ngAfterViewInit() {
    this.steps.changes.subscribe(steps => {
        console.log(steps);
    });
}

The error I'm getting is: 我得到的错误是:

metadata_resolver.js:639 Uncaught Error: Can't construct a query for the property "steps" of "Sequence" since the query selector wasn't defined. metadata_resolver.js:639未捕获的错误:由于未定义查询选择器,因此无法为“序列”的“步骤”属性构造查询。

whereas both Sequence and SequenceStep components do have their selectors defined in their @Component decorators ( sequence and sequence-step respectively). SequenceSequenceStep组件的确在其@Component装饰器中定义了它们的选择器(分别为sequencesequence-step )。

What am I doing wrong here? 我在这里做错了什么?

您是否尝试过在@ViewChildren arg中添加引号?

@ViewChildren('SequenceStep') steps: QueryList<SequenceStep>;

There are a few things 有几件事

  • If you pass children from the outside, they are content not children. 如果您从外面传递孩子,他们不是孩子。 @ViewChild() only works for children added to the template of a component directly. @ViewChild()仅适用于直接添加到组件模板中的子级。

    @ContentChildren() works on transcluded content: @ContentChildren()处理被包含的内容:

     @ContentChildren(TheChild) kids: QueryList<TheChild>; 
  • this.kids.changes() only notifies about changes after the initialization. this.kids.changes()仅在初始化后通知更改。 Therefore just access this.kids directly in ngAfterViewInit() and then subsribe to get notified about later changes 因此,只需直接在ngAfterViewInit()访问this.kids ,然后订阅即可获得有关以后更改的通知

     ngAfterViewInit() { this.myKidsCount = this.kids.length; this.cdRef.detectChanges(); this.kids.changes.subscribe(kids => { this.myKidsCount = kids.length; }); } 
  • Angular doesn't like when change detection causes changes. 当更改检测导致更改时,Angular不喜欢。 ngAfterViewInit() is called by change detection, this is why this.myKidsCount = this.kids.length causes an exception. ngAfterViewInit()由更改检测调用,这就是为什么this.myKidsCount = this.kids.length导致异常的原因。

To work around I invoke change detection explicitly after changing the property myKidsCount : 要变通,我在更改属性myKidsCount后显式调用了更改检测:

constructor(private cdRef:ChangeDetectorRef){}

ngAfterViewInit() {
  this.myKidsCount = this.kids.length;
  this.cdRef.detectChanges();
}

Plunker example 柱塞示例

此问题可能与SequenceStep的导入有关,请检查相关导入行的类名称。

@ViewChildren('SequenceStep') steps: QueryList<SequenceStep>;

Worked for me. 为我工作。 Angular 4.XX Angular CLI 1.XX Angular 4.XX Angular CLI 1.XX

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

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