简体   繁体   English

如何从孩子接收 $emitted 数据到父母?

[英]How to receive $emitted data from child to parent?

From the child component, I am emitting an event fetchtab , along with some data just like that:从子组件中,我发出一个事件fetchtab ,以及一些类似的data

this.$emit('fetchtab', data);

I want that data to be assigned to a currentData prop of my parent component (the root component).我希望将该data分配给我的父组件(根组​​件)的currentData道具。

data: {
    currentData: {}
}

This data is an array of objects.data是一个对象数组。 I need to iterate over that array in my parent component:我需要在我的父组件中迭代该数组:

<ul>
    <li v-for="item in currentData">@{{ item.name }}</li>
</ul>

But I can't figure out how do I fill in the currentData in the first place.但我不知道如何首先填写currentData I know I could use a) @fetchtab="myMethod" or, b) this.$on('fetchtab', fn)我知道我可以使用 a) @fetchtab="myMethod"或 b) this.$on('fetchtab', fn)

But where and how do I use it?但是我在哪里以及如何使用它?

You need to write the @fetchtab event in your parent component as:您需要在父组件中编写 @fetchtab 事件:

<parent-component @fetchtab="myMethod"></parent-component>

For more details have a look at Reference .有关更多详细信息,请查看参考

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

相关问题 Vue.js 中的子组件未向父组件发出数据 - Data not emitted from child to parent component in Vue.js 从孩子到父母的发射事件不起作用 - Emitted event from child to parent not working VueJS V-bind:sync=&quot;{object}&quot; -- 在父节点中观察子节点发出的数据 - VueJS V-bind:sync="{object}" -- watching data emitted from child in parent 子组件可以从父组件接收旧数据进行操作吗? - Can the child component receive old data for manipulation from parent component? 从由父选项卡打开的子选项卡页面接收数据-Javascript - Receive data from a child tab page opened by the parent tab - Javascript Vue 父组件无法捕获子组件发出的事件 - Vue parent component unable to catch event emitted from the child 如何在AngularJS中将数据从父级发送到子级 - How to send data from the Parent to the Child in the AngularJS 从Vue js中发出的子组件方法中更改父属性 - Change parent property from within emitted child component method in Vue js 在父级中单击按钮时如何将数据从子级传递给父级 - How to pass data from child to parent when button clicked in parent 在子组件(Angular)发出 selectAll 和 unSelectAll 事件后在父组件中获取结果 - get result in parent component after selectAll and unSelectAll event emitted from child (Angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM