如何在vue中将数据从子组件传递给父组件? [英] How to pass data from child to parent component in vue?
问题描述
我正在使用 Vue.Js,我从父组件多次调用我的子组件.这意味着为所有不同的调用创建了单独的实例.数据json"将包含所有不同实例的单独值.现在我想从父组件中获取所有子组件实例中变量 json 中存在的数据.
<代码>[代码]父组件<div v-for="(value, index) in input" :key="index++"><ChildComponent :componentcount="index" ></ChildComponent>
子组件<模板><div id="你好"><div><v-text-field :id="'ComponentHeader_' + $attrs.componentcount" v-model="header"class="headertag" label="Child Tag" @change="createJson" 概述></v-text-field>
</模板><脚本>导出默认{数据(){返回{json:"",}}}
为此您可以使用 $emit 方法.v-on 指令捕获 $emit 发出的子组件事件子组件触发 clicked 事件:导出默认{方法: {onClickButton(事件){this.$emit('clicked', 'someValue')}}}父组件接收点击事件:<div><child @clicked="onClickChild"></child>