简体   繁体   English

如何在不使用angular7中的事件发射器的情况下将数据从子组件传递到父数据?

[英]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.

相关问题 如何将数据从父组件传递到子组件? - How to pass data from parent to child components? 如何使用EventEmitters从子页面到父页面进行通信 - How to communicate from Child to Parent page using EventEmitters 如何在 Angular 7 中将数据从子级传递给父级 - How to pass data from child to Parent in Angular 7 Angular 9 - 如何将文本数据数组从父组件传递到多层子组件? - Angular 9 - How to pass array of text data from parent component to multiple layers of child components? 当父级有动作时,Angular7将新数据传递给子组件 - Angular7 pass new data to child component when parent has action Angular中如何将父组件的数据共享给多个子组件? - How to share the data from Parent Component to Multiple Child components in Angular? 如果它们不是Angular2中的父子组件,如何在组件之间传递数据? - How to pass data between components, if they are not parent-child components in Angular2? 无法使用@ViewChild Angular 7将数据从子级传递到父级 - Can't pass data from Child to Parent using @ViewChild Angular 7 在Angular中将父节点之间的异步数据传递给子节点 - Pass async data from parent to child in Angular 如何在 Angular 中将数据从父级传递给子级 - How to Pass Data from from Parent to Child in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM