Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VUEJS remove Element From Lists?

Tags:

vue.js

it is possible to remove specific element from lists. i tried this functions for remove element

pop() = remove last element

$remove(index) = not remove any element from lists

remove( index ) = undefined function

unshift( index ) = add new and empty element

splice( index ) = remove all element from index

please help me to remove specific element from lists.

below is my js code

var example2 = new Vue({   el: '#example-2',   data: {     items: [       { message: 'Foo' },       { message: 'Bar' },       { message: 'Bar1' },       { message: 'Bar2' },       { message: 'Bar3' },       { message: 'Bar4' }     ]   },   methods : {     removeElement : function(index){         this.items.$remove(index);     }   } }) 

below is my HTML code

<ul id="example-1">   <li v-for="(key, item) in items">     {{ item.message }}     <button v-on:click="removeElement(key)">remove</button>   </li> </ul> 
like image 923
Renish Khunt Avatar asked Feb 17 '16 14:02

Renish Khunt


People also ask

How do I remove items from my Vue list?

Bind the click handler to the delete icon created in step 1. Within the click event, remove the list item by passing the delete icon list item to removeItem method.

How do you remove a Vue router?

We can remove the hash from the URLs with Vue Router 4 by calling the createWebHistory method. In main. js , we have the routes array with the route definitions. We map the URL paths to components.

How do I remove a plugin from Vue?

Under "Dependencies" (second tab on the left of vue ui) you'll find all plugins listed. And on the right of each plugin there is a little trash icon, which removes the respective plugin.


1 Answers

$remove is deprecated in Vue.js 2.0 and replaced by splice as stated in the docs. Make sure you add the 2nd parameter of splice for it to work.

Migration From Vue 1.x - 2.0

methods: {   removeElement: function (index) {     this.items.splice(index, 1);   } } 
like image 55
jonan.pineda Avatar answered Sep 30 '22 16:09

jonan.pineda