简体   繁体   English

无法从父组件访问子组件功能

[英]Can't able to access child component function from parent component

I have a separate assetAnnotationComponent(child) which manages the annotating functionality.This component is called from my main component to perform annotation on images by mouse events.Iam getting the following error when accessing the assetAnnotationComponent object我有一个单独的 assetAnnotationComponent(child) 来管理注释功能。这个组件是从我的主组件调用的,以通过鼠标事件对图像执行注释。访问assetAnnotationComponent对象时出现以下错误

ERROR TypeError: Cannot read property 'mychildFunction' of undefined

I tried like following in my parent component @ViewChild(AssetAnnotationComponent) assetAnnotationComponent我尝试在我的父组件@ViewChild(AssetAnnotationComponent) assetAnnotationComponent

Here is parent component HTML这是父组件 HTML

<div class="modal-body">
  <app-asset-annotation></app-asset-annotation>
  </div>

Here is parent component typescript这是父组件打字稿

import { AssetAnnotationComponent } from './asset-annotation.component';

export class ParentComponent{
@ViewChild(AssetAnnotationComponent) assetAnnotationComponent;

ngAfterViewInit(){
this.assetAnnotationComponent.mychildFunction();
}
}

my child component我的子组件

export class AssetAnnotationComponent{

mychildFunction()
{
console.log("success");
}

}

I am expecting "success" to printed in console so that i can be accessing the child component's function.Thanks in advance我期待在控制台中打印“成功”,以便我可以访问子组件的功能。提前致谢

Edit: As suggested in comments and from the question that is suggested as duplicate, i have already tried that.编辑:正如评论和建议重复的问题所建议的那样,我已经尝试过了。 I am using ngViewAfterInit for calling my function so view is loaded completely.我正在使用 ngViewAfterInit 来调用我的函数,以便完全加载视图。 Edit 2: My child component is from different module编辑 2:我的子组件来自不同的模块

Edit 2: My child component is from different module编辑 2:我的子组件来自不同的模块

here is my sample working code 这是我的示例工作代码

In my code if the selector of the child component is added in parent component html then i get desired output but i need to access the child component function without adding selector of child component in parent component在我的代码中,如果在父组件 html 中添加了子组件的选择器,那么我会得到所需的输出,但我需要访问子组件功能而不在父组件中添加子组件的选择器

in your parent component do this:在您的父组件中执行以下操作:

@ViewChildren('myChild') assetAnnotationComponent: QueryList<AssetAnnotationComponent>; 
public currentChildComponent: AssetAnnotationComponent;

    ngAfterViewInit(): void {
          this.assetAnnotationComponent.changes.subscribe(data => {
              this.currentChildComponent= data._results[0];
              this.currentChildComponent.mychildFunction();
          });
    }

Parent HTML do this:父 HTML 这样做:

<div class="modal-body">
  <app-asset-annotation #myChild></app-asset-annotation>  //notice the template variable
</div>

Try giving a reference to the child:尝试给孩子一个参考:

<app-asset-annotation #child></app-asset-annotation>

and then in the .ts:然后在 .ts 中:

@ViewChild('child') child;

then you could call it as:那么你可以称之为:

this.child.mychildFunction();

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

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