Noob Coder Noob Coder - 1 month ago 21
Javascript Question

vuejs this.posts.$remove(postId) is not a function

im trying to remove an array element using $remove. but it says this.posts.$remove is not a function. Can anyone explain where am i wrong?

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


vue instance:

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


this is my example data

my data

this is my console

enter image description here

Answer

I see in the tags that you are using VueJS 2. The $remove() method has been deleted: http://vuejs.org/v2/guide/migration.html#Array-prototype-remove-removed

As said in the migration guide, you should just use the splice() method:

methods: {
  removeTodo: function (todo) {
    var index = this.todos.indexOf(todo)
    this.todos.splice(index, 1)
  }
}