deleteData()
从同时用于创建动态组件的对象数组中删除元素后,我无法处理正确的渲染。
元素被正确删除,但它表现得好像组件不知道它应该刷新,并且 Vue 不知道数据已更改。
在chrome Vue扩展中,移除了正确的元素,但是DOM中的Vue移除了最后一个element()
视图:
<template>
<b-container>
<SectionSelector :AddSection="AddSection"/>
<component v-for="(section, index) in sections"
:key="index"
:is="section.type"
:sectionIndex="index"
:sectionData="section[index]"
:deleteData="deleteData"
@sectionDataEmit="sectionDataEmit"/>
</b-container>
</template>
<script>
import SectionSelector from './components/SectionSelector.vue';
import FullText from './components/sections/FullText.vue';
import FullImage from './components/sections/FullImage.vue';
import ImageRightTextLeft from './components/sections/ImageRightTextLeft.vue';
import ImageLeftTextRight from './components/sections/ImageLeftTextRight.vue';
import Vue from 'vue'
export default {
data() {
return {
sections: []
}
},
methods: {
AddSection(sectionData) {
this.sections.push(sectionData);
},
updateSection(sectionIndex, sectionData) {
Vue.set(this.sections, sectionIndex, sectionData);
},
sectionDataEmit(emitData) {
Vue.set(this.sections, emitData.position, emitData.content);
},
deleteData(index) {
// eslint-disable-next-line
console.log(index)
this.$delete(this.sections, index);
}
},
components: {
SectionSelector,
FullText,
FullImage,
ImageRightTextLeft,
ImageLeftTextRight
}
}
</script>
零件:
<template>
<b-row>
<h3>Full text {{ sectionIndex+1 }}</h3>
<b-button variant="danger"
@click="deleteButton(sectionIndex)">delete</b-button>
<b-textarea :value="sectionData"
@input="sectionDataEmit" />
</b-row>
</template>
<script>
export default {
props: ['sectionIndex', 'sectionData', 'deleteData'],
methods: {
sectionDataEmit(value) {
let emitData = {
position: this.sectionIndex,
content: {
type: 'FullText',
fields: {
text: value
}
}
}
this.$emit('sectionDataEmit', emitData)
},
deleteButton(index) {
this.deleteData(index)
}
}
}
</script>