【问题标题】:using `ViewChild` - method is not calling the child method使用 `ViewChild` - 方法没有调用子方法
【发布时间】:2018-05-10 06:34:14
【问题描述】:

我正在尝试从父级调用我的子组件方法,但根本不起作用。

这是我的代码:

<router-outlet>
   <app-modal-popup></app-modal-popup> //children for all pages!!
</router-outlet>

在首页:

    import { Component, OnInit, Output, ViewChild, AfterViewInit  } from '@angular/core';
    import { ModalPopupComponent } from '../../shared/modal/modal-popup/modal-popup.component';

    export class HomeComponent implements OnInit, AfterViewInit {

        @ViewChild('appModal') private appModal : ModalPopupComponent;

    footerLinkHandler(link){
        console.log('link', link );
        this.appModal.popMessage(link);
        return false;
    }

}

但收到Cannot read property 'popMessage' of undefined 的错误 - 有什么问题。但我从appModal 得到popMessage 自动填充。

【问题讨论】:

    标签: angular


    【解决方案1】:

    试试这个

    @Component({
     ...
    directives: [ModalPopupComponent]
    })
    
    class HomeComponent implements OnInit, AfterViewInit{
     @ViewChild(ModalPopupComponent) childComp:ModalPopupComponent;
     ...
     footerLinkHandler(link){
        console.log('link', link );
        this.childComp.popMessage(link);
        return false;
     }
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2023-03-09
      • 2012-03-13
      • 2012-08-25
      相关资源
      最近更新 更多