I have an array of quotes in my parent template which I pass to a child component as a prop. I then take that array and create another child component for each index in the array by passing it through a for loop into a slot. This all seems to work fine.
The problem is that I am trying to delete each index of the array on clicking it's element, but whenever I click an element the first index is deleted, not the index linked to the element clicked.
In the following component I execute method @click and run the method deleteQuote passing the related index.
<template>
<div class="panel-body" @click="deleteQuote">
<slot></slot>
</div>
</template>
<script type="text/javascript">
export default{
methods: {
deleteQuote() {
this.$emit('deleteThisQuote', this.index)
}
},
props: ['index']
}
</script>
Since this component doesn't have direct access to my array, I emit a custom event that is listened for in its parent. This triggers another method which splices my array at this.index. Following is the parent component...
<template>
<div>
<quote-singles
class="col-sm-3 panel panel-default"
v-for="(quote, index) in quotes"
:index="index"
@deleteThisQuote="deleteQuote($event)"><h3>{{index}}{{quote}}</h3>
</quote-singles>
</div>
</template>
<script type="text/javascript">
import quoteSingles from './quoteSingles.vue'
export default{
methods: {
deleteQuote(event) {
this.quotes.splice(this.event, 1);
}
},
props: ['quotes'],
components: {
'quoteSingles': quoteSingles
}
}
</script>
This does everything I want, except it splices out the wrong index in the array. No matter which element I click, myArray[0] is removed. What is going on here? Am i missing something elementary?