[英]How to pass the data from child components to parent data without using eventemitters in angular7?
I need to get the data from child components oninit only. 我只需要从子组件oninit获取数据。 How to get that?
如何获得? Is it Possible or not?
有可能吗?
When visiting the child component, parent components needs to print the name of that child component. 当访问子组件时,父组件需要打印该子组件的名称。 How to do that ?
怎么做 ?
One way you can do this is using @ViewChild : 一种方法是使用@ViewChild :
Add @ViewChild in your parent component: 在您的父组件中添加@ViewChild:
export class ParentComponent {
...
@ViewChild(ChildComponent) child: ChildComponent;
...
Now on parent's ngOnInit
you will have access to your child component: 现在,在父级的
ngOnInit
您可以访问子级组件:
ngOnInit() {
// this.child - will give you access to your child component
}
Check this StackBlitz DEMO to see this in action
检查此StackBlitz DEMO以查看此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.