vue:拼接不适用于当前元素

vue: splice does not work on the current element

我有一个这样的对象数组:

  daySelected: [
    {
      selectDay: day,
      time: [
        {
          defaulTimeStart: "08:00",
          defaultTimeEnd: "00:00",
        }
      ]
    },
    {
      selectDay: day,
      time: [
        {
          defaulTimeStart: "08:00",
          defaultTimeEnd: "00:00",
        }
      ]
    },
     //etc....
  ];

数组是用这个函数形成的,它在点击时推送一个对象:

 const object = {
    selectDay: day,
    time: [
      {
        defaulTimeStart: "08:00",
        defaultTimeEnd: "00:00",
      },
    ], 
  };
 this.daySelected.push (object);

到目前为止一切正常...当我想删除当前推送的元素时出现问题:

 this.daySelected.splice(this.daySelected.indexOf(object), 1);

如果我单击数组末尾的元素将其删除,它会起作用,但如果我单击中间元素或数组开头,它会删除下一个...

与其通过 this.daySelected.indexOf(object) 指示索引,不如尝试直接从 v-for 指示索引。 您可以使用 v-for 迭代 daySelected 数组,如下所示:

<div v-for="(obj, index) in daySelected">
...
// use `index` to pass as a parameter of the `splice` method.
... deleteObj(index) ...
...
</div>
<script>
...
  methods: {
    deleteObj(index) {
       this.daySelected.splice(index, 1);
    }
  }
...
</script>