H
H
hydra_132019-04-03 16:11:41
Vue.js
hydra_13, 2019-04-03 16:11:41

How to add interactivity to the created records using v-for (namely, when the mouse button is pressed, call the function)?

Actually the question is in the title.
I'm trying to get interactivity for the delete key and trash can in a tutorial project.
Vue.js is just getting started.

Here is the html part:

<div class="basket">
     <div class="basket-goods-list">
            <basket v-for="item in goodsListItems" v-bind:item="item" v-bind:key="item.name">
            </basket>
     </div>
</div>


and here is the js code:
Vue.component('basket', {
    props: ['item'],
    template: `
    <div class="basket-goods-list_item">
        <span class="basket-goods-list_item-txt">
            {{item.name}}: цена: {{item.price}}; кол-во: {{item.count}}
        </span>
        <div class="basket-goods-list_item-del-btn" 
            @click="removeFromBasket">
            Удалить
        </div>
    </div>`
})

let basketElement = new Vue({
    el: '.basket',
    data: {
        goodsListItems: basket.goodList
    },
    methods: {
        removeFromBasket() {
            console.log('remove')
        }
    }
})


As a result, I see in the browser:
5ca4b081114e3428888995.png

But this is what is in the browser console:
5ca4b098195b6645659140.png
and the buttons do not work (in principle, it is clear why from the console)

I repeat the question: How can I add interactivity to the created records using v-for (namely, when the mouse button is pressed, call the function)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-04-03
@hydra_13

In the child component, handle the click on the delete button like this: @click="$emit('remove')".
And in the parent component subscribe to this event: @remove="removeFromBasket(item)".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question