阵列拼接总是从最后删除一个项目吗?

时间:2018-09-27 14:09:10

标签: javascript vue.js

我在从数组中删除项目时遇到问题。数组拼接应该可以工作,但是却不能像我想要的那样工作。它总是从最后删除项目。我正在使用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>

3 个答案:

答案 0 :(得分:2)

这一定是由于您的流程按钮所致,我试图复制您的错误,但最终遇到了这段代码。我只是将flow按钮替换为输入,它起作用了。尝试下面的代码。

使用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>

答案 1 :(得分:1)

我认为您可能会与组件的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>

答案 2 :(得分:0)

试试这个。使用此方法正确删除项目。

<div v-for="(item, ind) in buttons" :key="JSON.stringify(item)">