** 例如,在这里,当我单击按钮时,我将多一个组件,这意味着它将有新数据,所以我想在按下“保存数据”按钮时将所有信息收集到一个数组中,我希望,很容易理解
<Child v-for="count in btnNumber" :key="count" @showData="getElements" /> <v-btn color="primary" elevation="10" class="space" large @click="duplicateEl" >Add Categ & Key</v-btn > v-btn color="secondary" elevation="13" class="btnEl" dark large @click="getResult" >Save Data</v-btn
** 它使用 Emit 从我的子组件获取数据
methods:{ getElements(emitPayload) { this.selectedChildCategory = emitPayload.selectedCateg; this.selectedChildKey = emitPayload.selectedKey; this.selectedChildLanguage = emitPayload.selectedLang; this.selectedChildContent = emitPayload.selectedCon; } } duplicateEl() { this.btnNumber++; }
P粉4182142792024-03-28 09:10:08
您可以在父组件上保存数据,请查看以下代码片段:
Vue.component('Child', { template: ``, props: ['conte'], data() { return { content: this.conte, categories: ['first', 'second', 'third'], keys: [1,2,3], langs: ['g', 'h', 'j'], contents: ['aaa', 'bbb', 'ccc'] } }, methods: { setD() { this.$emit('show', this.content); }, }, }) new Vue({ vuetify: new Vuetify(), el: "#app", data() { return { contentFields: [{id: 0, cat: '', key: '', lang: '', cont: ''}], showData: false } }, methods: { addInput() { let newI = this.contentFields.length this.contentFields.push({id: newI, cat: '', key: '', lang: '', cont: ''}) }, getElements(e){ const newData = this.contentFields.map(obj => { if(obj.id === e.id) return { ...obj } return obj }); }, getResult() { this.showData = !this.showData } } })
[email protected]/css/materialdesignicons.min.css" rel="stylesheet"> [email protected]/dist/vuetify.min.css" rel="stylesheet">sssccc ssscccAdd Categ & Key Save Data {{ contentFields }}