2

我试图使用 $remove 删除数组元素。但它说 this.posts.$remove 不是一个函数。谁能解释我错在哪里?

<button type="button" class="btn btn-danger" @click="deletePost(post.id)">Xxx</button>

Vue 实例:

deletePost(postId){
        console.log(postId);
        this.posts.$remove(postId);
      },

这是我的示例数据

我的数据

这是我的控制台

在此处输入图像描述

4

1 回答 1

7

我在标签中看到您正在使用 VueJS 2。该$remove()方法已被删除:http: //vuejs.org/v2/guide/migration.html#Array-prototype-remove-removed

如迁移指南中所述,您应该只使用以下splice()方法:

methods: {
  removeTodo: function (todo) {
    var index = this.todos.indexOf(todo)
    this.todos.splice(index, 1)
  }
}
于 2016-11-07T18:05:28.917 回答