![](/img/trans.png)
[英]Angular 8: using ViewChild get a method result from a child to parent after that method is invoked in child component
[英]using `ViewChild` - method is not calling the child method
我正在尝试从父级调用我的子级组件方法,但是根本不起作用。
这是我的代码:
<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
出现错误-问题是什么。 但我得到popMessage
从appModal
为自动填充。
尝试这个
@Component({
...
directives: [ModalPopupComponent]
})
class HomeComponent implements OnInit, AfterViewInit{
@ViewChild(ModalPopupComponent) childComp:ModalPopupComponent;
...
footerLinkHandler(link){
console.log('link', link );
this.childComp.popMessage(link);
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.