[英]AngularJS / JavaScript Splice - Always deletes first or last item from array
[英]Array Splice always delete an item from last?
我在从数组中删除项目时遇到问题。 阵列拼接应该可以工作,但它不像我想要的那样工作。 它总是从最后删除项目。 我正在使用 Vue.js 。 我正在将项目动态推送到数组。 但是点击后从最后删除它的删除。 为什么我面临这个。 我正在附上代码。
<template>
<div>
<span class="badge badge-pill mb-10 px-10 py-5 btn-add" :class="btnClass" @click="addBtn"><i class="fa fa-plus mr-5"></i>Button</span>
<div class="block-content block-content-full block-content-sm bg-body-light font-size-sm" v-if="buttons.length > 0">
<div v-for="(item, index) in buttons">
<div class="field-button">
<div class="delete_btn"><i @click="remove(index)" class="fa fa-trash-o"></i></div>
<flow-button v-model="item.title" :showLabel="false" className="btn btn-block min-width-125 mb-10 btn-border" mainWrapperClass="mb-0" wrapperClass="pt-0" placeholder="Button Title"></flow-button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import flowButton from '../assets/flow-button'
export default {
name: "textArea",
props:{
index : Number
},
data() {
return {
buttons : [],
btnClass : 'badge-primary',
}
}
components : {
flowButton
},
methods : {
addBtn () {
if(this.buttons.length >= 2) {
this.btnClass = 'btn-secondary'
}
if(this.buttons.length < 3) {
this.buttons.push({
title : ''
});
}
},
remove(index) {
this.buttons.splice(index, 1)
}
}
}
</script>
这一定是因为你的流程按钮,我试图复制你的错误,但最终还是这个代码。 我只是用输入替换了流程按钮,它可以工作。 试试下面的代码。
使用 v-bind:key="index",当 Vue 更新使用 v-for 呈现的元素列表时,默认情况下它使用“就地补丁”策略。 如果数据项的顺序发生了变化,Vue 不会移动 DOM 元素以匹配项的顺序,而是就地修补每个元素并确保它反映应该在该特定索引处呈现的内容。 这类似于 track-by="$index" 的行为
您在数据和组件之间缺少逗号,我在这里删除了组件,它现在不会导致任何错误,并且更多提示不要将双引号与单引号混合。
<template>
<div>
<span class="badge badge-pill mb-10 px-10 py-5 btn-add" :class="btnClass" @click="addBtn"><i class="fa fa-plus mr-5"></i>Button</span>
<div class="block-content block-content-full block-content-sm bg-body-light font-size-sm" v-if="buttons.length > 0">
<div v-for="(item, index) in buttons" v-bind:key="index">
<div class="field-button">
<div class="delete_btn"><i @click="remove(index)" class="fa fa-trash-o">sdfsdff</i></div>
<input type="text" v-model="item.title" :showLabel="false" className="btn btn-block min-width-125 mb-10 btn-border" mainWrapperClass="mb-0" wrapperClass="pt-0" placeholder="Button Title"/>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'textArea',
props: {
index: Number
},
data () {
return {
buttons: [],
btnClass: 'badge-primary'
}
},
methods: {
addBtn () {
if (this.buttons.length >= 2) {
this.btnClass = 'btn-secondary'
}
if (this.buttons.length < 3) {
this.buttons.push({
title: ''
})
}
},
remove (index) {
this.buttons.splice(index, 1)
}
}
}
</script>
我认为您可能正面临与组件的index
属性冲突。 尝试为v-for
循环的索引使用不同的名称:
<div v-for="(item, ind) in buttons">
<div class="field-button">
<div class="delete_btn"><i @click="remove(ind)" class="fa fa-trash-o"></i></div>
<flow-button v-model="item.title" :showLabel="false" className="btn btn-block min-width-125 mb-10 btn-border" mainWrapperClass="mb-0" wrapperClass="pt-0" placeholder="Button Title"></flow-button>
</div>
</div>
尝试这个。 使用此正确删除项目。
<div v-for="(item, ind) in buttons" :key="JSON.stringify(item)">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.