Vue Js Delete Element in Array Reactively Example

Vue.Js

Nicesnippets

412

10-08-2020


Vue Js Delete Element in Array Reactively Example

Hi Guys,

In this example, I will explain you how to vue js remove element in array reactively. I will show element in delete first and last using vue js. you can easyliy delete array in element using vue js. i will vue js delete element in array reactively using shift and pop.

Here, I will give you full example for simply delete element in array reactively using vue.js as bellow.

Script Code


<script>

Vue.config.devtools = true

var app = new Vue({

el:'.card-body',

data:{

items:[

{ message: 'Php' },

{ message: 'Laravel'},

{ message: 'Java'},

{ message: 'Html'},

],

},

methods: {

deleteFirst() {

this.items.shift();

// delete last

},

deleteLast() {

this.items.pop();

// delete last

}

}

})

</script>

Full Example

<!DOCTYPE html>

<html>

<head>

<title>Vue Js Delete Element in Array Reactively Example</title>

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

</head>

<body class="bg-dark">

<div class="container">

<div class="col-md-6 offset-md-3">

<div class="card mt-5">

<div class="card-header">

<h5 v-bind:class="red">Vue Js Delete Element in Array Reactively Example</h5>

</div>

<div class="card-body">

<div class="row">

<div class="col-md-12">

<ul>

<li v-for="item in items" v-text="item.message"></li>

</ul>

<button class="btn btn-outline-primary" @click="deleteFirst">

Delete First

</button>

<button class="btn btn-outline-danger" @click="deleteLast">

Delete Last

</button>

</div>

</div>

</div>

</div>

</div>

</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>

<script>

Vue.config.devtools = true

var app = new Vue({

el:'.card-body',

data:{

items:[

{ message: 'Php' },

{ message: 'Laravel'},

{ message: 'Java'},

{ message: 'Html'},

],

},

methods: {

deleteFirst() {

this.items.shift();

// delete last

},

deleteLast() {

this.items.pop();

// delete last

}

}

})

</script>

</body>

</html>

It will help you..


Recommended Posts