I'm making a game.
- A controller adds "coins" to the data.
- When a coin is added, it mounts a component, which html gets animated (simple sprite animation with GSAP)
- When the animation is over the coin needs to get deleted from the data.
This is where I struggle. I don't know how to delete the array-item from the data; does "mounted()" know about which array key?
I thought about passing the array-index to the component; the indizes constantly change; coins get added and deleted randomly.
I tried using splice()
as suggested in here: How to remove a item from a array Vue.js, but couldn't get it to work.
Since I guess this is a pretty usual use-case I figured if there is a straight forward solution to this.
My html:
<div class="game">
<xyz v-for="coin in coins" v-bind:data="coin" is="coin"></xyz>
</div>
My js:
// Simplified component
// of course the coin has data like value etc.
Vue.component("coin", {
props: ["data"],
template: `
<div class="sprite">
<img :src="coin' + data.lane . '.png" />
</div>
`,
mounted () {
// Sprite animation
var TweenMax ...
.eventCallback("onComplete", function() {
// (!)
// > delete the data entry, that triggered this rendering
// (!)
})
}
})
// Vue root instance
var game = new Vue({
el: ".game",
data() {
coins: [
{ lane: 3 },
{ lane: 2 },
...
]
}
})