How do you get $images into your component?
Oct 16, 2016
17
Level 11
Remove item from array (Vue 2)
Hi guys,
I am trying to remove an item from an array, the old $remove method has been removed from Vue js 2.0, so I am trying to use array splice instead, the item is removed, but the UI is not showing the change until i refresh my browser.
deleteImage: function(index, image) {
var accepted = confirm('Do you really want to delete the image?');
if(accepted) {
var form = new FormData();
form.append('imageId',image.id);
form.append('path', image.path);
console.log(index);
this.$http.post('/delete-image', form).then(function(response) {
this.images.splice(index, 1, image);
});
}
}
How do you remove items in your Vue 2 applications?
Level 2
Use filter instead of splice, it will replace the array and trigger an update:
this.images = this.images.filter(function (item) {
return image.id != item.id;
});
5 likes
Please or to participate in this conversation.