【问题标题】:Why is Vue .splice() always removing the last item为什么 Vue .splice() 总是删除最后一项
【发布时间】:2020-05-09 03:13:53
【问题描述】:

更新:现在可以使用了。我不确定我做了什么 - 老实说,我不记得有任何改变,但我确信这是由于反馈 - 所以感谢所有提供帮助的人。

我有一个循环,它根据数组中的对象生成表行。每当我触发删除时,无论我要删除哪一行,它始终是数组中被删除的最后一项。

数据 - 存储为“会议计划”

[
  {
    dow:"-T-R---",
    endTime:"11:45:00",
    startTime:"08:45:00"
  },
  {
    dow:"--W----",
    endTime:"12:45:00",
    startTime:"10:45:00"
  },
  {
    dow:"----F--",
    endTime:"15:00:00",
    startTime:"14:00:00"
  }
]

模板

<tr v-for="(meetingPlan,meetingPlanIndex) in meetingPlans" :key="meetingPlanIndex">
    <td>
      -- stuff here doesn't matter --
    </td>
    <td>
        -- stuff here doesn't matter either --
    </td>
    <td class = "pl-0">
        <button v-on:click="removeMeetingPlan(meetingPlanIndex)" type = "button" class = "btn btn-danger btn-sm">
            <i class = "fas fa-times"></i>
        </button>
    </td>
</tr>

Vue 代码

var vm = new Vue({
        el:"#dynamic-planning-column",
        delimiters:["<%","%>"],
        // This is defined server-side.  "meetingPlans" is one of the properties of planData
        data:window.planData,
        methods:{
            addReserveGroup:function(index){
                this.reserveGroups.push({number:'',description:'',cap:''});
            },
            removeReserveGroup:function(index){
                this.reserveGroups.splice(index,1);
            },
            addMeetingPlan:function(index){
                this.meetingPlans.push({id:0,dow:'',startTime:'',endTime:'',timeslot:-1});
            },
            removeMeetingPlan:function(index){
                this.meetingPlans.splice(index,1);
            },
            meetingPlanDowIsOdd:function(MeetingPlan){
                return MeetingPlan.dow.includes('M') || MeetingPlan.dow.includes('W') || MeetingPlan.dow.includes('F');
            }
        }
    });

在我看来,这应该可行。

我尝试过的调试

  • 确保模板中的meetingPlanIndex 是唯一值。
  • 当我将removeMeetingPlan() 修改为输出index 和输出this.meetingPlans[index] 时,两者都是预期值。
  • 当我将 removeMeetingPlan() 修改为硬编码时:this.meetingPlans.splice(0,1) - 它仍然会删除最后一项

我尝试过的解决方案 我读到可以将:key 设为对象而不是数字,因此我将模板修改为:

<tr v-for="(meetingPlan,meetingPlanIndex) in meetingPlans" :key="meetingPlan">

这样做的目的是为了删除正确的行,但 Vue 抛出了关于重复键的警告,并使用非基元作为键。


我还能尝试什么?

【问题讨论】:

  • 当你说'它总是被删除的数组中的最后一个项目'时,你是如何确定的?听起来您正在成功修改数组,但由于key 使用不当,更新后的数组未正确呈现。
  • 我无法重现这个问题,但是,字符串化对象应该不会失败。
  • 可能将:key="meetingPlanIndex" 更改为:key="JSON.stringify(meetingPlan)"
  • @Quasipickle 听起来您的 &lt;td&gt; 元素中有状态组件,但您尚未发布它们,因此很难分辨。我怀疑如果您尝试使用&lt;td&gt;{{ meetingPlan }}&lt;/td&gt;(出于调试目的),您会发现即使没有任何key 魔法它也能正常工作。如果您有连续编辑数据的功能,那么您可能会发现JSON.stringify 引入了一大堆新问题。如果对象的属性没有改变,它应该可以正常工作。
  • “当我将 removeMeetingPlan() 修改为硬编码时:his.meetingPlans.splice(0,1) - 它仍然删除最后一个项目”结合上述 meetingPlans 的无效定义(每个字段不是逗号分隔的)让我觉得你的数组有异常。

标签: vue.js vuejs2


【解决方案1】:

我可以用你提供的代码做一个例子 - 似乎工作正常。

Vue.component('meeting-plans', {
  data: function() {
    return {
      meetingPlans: [
        {
          dow: "-T-R---",
          endTime: "11:45:00",
          startTime: "08:45:00",
        },
        {
          dow: "--W----",
          endTime: "12:45:00",
          startTime: "10:45:00",
        },
        {
          dow: "----F--",
          endTime: "15:00:00",
          startTime: "14:00:00",
        }
      ],
    }
  },
  methods: {
    removeMeetingPlan: function(index) {
      this.meetingPlans.splice(index, 1);
    },
  },
  template: `
    <table>
      <tr v-for="(meetingPlan,meetingPlanIndex) in meetingPlans" :key="meetingPlanIndex">
        <td class="pl-0">
          <button v-on:click="removeMeetingPlan(meetingPlanIndex)" type="button" class="btn btn-danger btn-sm">
            Remove {{ meetingPlan.dow }}
          </button>
        </td>
      </tr>
    </table>
  `
})

new Vue({
  el: '#demo',
})
<div id="demo">
  <meeting-plans />
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>

【讨论】:

  • 天啊!正确的。缺少逗号只是由于我的抄写错误。固定。
  • 我可以很容易看到的唯一区别是(1)您创建了一个组件,而我只是使用原始应用程序(即:var vm = new Vue({...}) )。此外,您的数据是一个函数,而我的只是一个对象 - 但我认为这是由于组件/应用程序的差异。这应该有什么明显的效果吗?
  • (1) 我认为这不会有什么不同。 (2) 可能是问题。数据必须是函数。 vuejs.org/v2/guide/components.html#data-Must-Be-a-Function
猜你喜欢
  • 2013-08-20
  • 2020-11-30
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多