【问题标题】:cannot call method from @viewchildren in parent无法从父级中的@viewchildren 调用方法
【发布时间】:2018-12-05 12:06:50
【问题描述】:

我正在尝试从父组件中的子组件调用方法,因为我有父组件的 html 代码

 <div class="right view-calendar">
       <child *ngFor="let selectedMonth of selectedMonths" [viewDate]="selectedMonth" [monthList]="show()"></child>
 </div>

父组件:

export class ParentComponent implements OnInit {
      @ViewChildren(MonthHeaderComponent) months: any[];
        show() {
         this.months.method();
       }
    }

子组件

export class ChildComponent implements OnInit {
   @Input() monthsList: Date[]
   method() {
   // code here....
   }
}

我已阅读此问题Can't get to @ViewChildren in parent component,但我不知道是否有什么可以帮助我。

我在这里做错了什么?

【问题讨论】:

  • 好吧告诉我们会发生什么。 “我在努力”……“我不知道有什么可以帮助我的”……“我做错了什么”。我们无法读懂你的想法。
  • @LazarLjubenović 具有 Angular 技能的人很容易理解这个问题。由于某些原因,OP 找不到他的错误,这就是他寻求帮助的原因。如果他知道他的问题,他就不会发布问题了:)
  • @trichetriche 我一个“具有 Angular 技能”的人。我并不是说 OP 应该知道原因。如果您需要帮助,我的意思是至少发布错误消息。甚至有错误消息吗?它会吞下错误吗?期望什么?发生了什么事——你的电脑爆炸了还是怎么了?它与 Angular 无关,与寻求帮助的基本知识有关。
  • 抱歉,这不是针对你的,我与作者无关,除非我真的需要,否则我往往不会看到姓名或个人资料。我同意你的观点,这些是基础,我不会反对。但考虑到我在这里度过的大量时间,以及我整天看到的所有类型的问题,我认为这个问题是“可以回答的”,并且认为没有必要提供更多背景信息。这是我对此事的两分钱,再次抱歉显得粗鲁!

标签: angular


【解决方案1】:

您有错误的类型,并试图调用子列表上的方法,而不是子本身。

@ViewChildren(MonthHeaderComponent) months: QueryList<MonthHeaderComponent>;

我建议您使用模板变量来简化您对问题的理解:

<child *ngFor="..." #children [viewDate]="selectedMonth" [monthList]="show(children)"></child>
show(child) {
  child.method();
}

【讨论】:

    猜你喜欢
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    相关资源
    最近更新 更多