Cannot import child component to parent component [duplicate]

我的梦境 提交于 2019-12-13 09:13:58

问题


I am using Angular 5. I want to call the child component method from parent component. For this I am adding the import {myChildComponent} from './myChild/myChild.component'; //ERROR: Cannot find module ./myChild/myChild.component

How can I import the child component in any other component other than app.component.ts.(here it works)


回答1:


You can simply call function of child component from parent component using local variable in Angular.

For example

<hello name="{{ name }}" #childOne></hello>
<p (click)='childOne.callFromParent()'>
  Start editing to see some magic happen :)
</p>

here hello is child component.

Working Example

Second way

<hello name="{{ name }}" #childOne></hello>
<button (click)='callChildFunction()'>hello</button>

@ViewChild('childOne') childOne: any;

callChildFunction() {
    this.childOne.callFromParent();
  }


来源:https://stackoverflow.com/questions/50739511/cannot-import-child-component-to-parent-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!