** 例如,在這裡,當我單擊按鈕時,我將多一個組件,這意味著它將有新數據,所以我想在按下“保存數據”按鈕時將所有資訊收集到一個數組中,我希望,很容易理解
<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 }}