简体   繁体   English

如何在 Vue.js 中获取从子组件到父组件的 json 收集的数据?

[英]How can I get json collected data from child to parent component in Vue.js?

I have results data in child component but I should bring it to main component.我在子组件中有结果数据,但我应该把它带到主组件。

Main Component in Parent, so each time I click the button, the result should be gathered in main app父组件中的主组件,因此每次单击按钮时,结果都应收集在主应用程序中

 <button @click="showFinalResult">Click me</button>

Child component data here and I just need to show results in json format in parent, the results are input results子组件数据在这里,我只需要在父组件中显示json格式的结果,结果是输入结果

 results: [],

You can emit event for child for sending data:您可以为孩子发出事件以发送数据:

 Vue.component('Child', { template: ` <div class=""> </div> `, data() { return { results: [1,2,3] } }, mounted() { this.$emit('update', this.results); } }) new Vue({ el: '#demo', data() { return { res: [], show: false } }, methods: { getResult(res) { this.res = res }, showResults() { this.show =.this.show } } }) Vue.config.productionTip = false Vue.config.devtools = false
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="demo"> <button @click="showResults">Click me</button> <p v-if="show">{{ res }}</p> <child @update="getResult" /> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM