dalexgo
dalexgo

Reputation: 41

How get key value in vuejs in v-for directive?

i have this on vue template:

<div v-for="item in items" :key="item.id">
  <!-- content -->
</div>

i want get the value of item.id and send via axios.

i dont know how bind de value from template to script section.

Upvotes: 4

Views: 3591

Answers (1)

dvnguyen
dvnguyen

Reputation: 3022

You can put a button with a on click handler inside the div:

<div v-for="item in items" :key="item.id">
  <button @click="sendItem(item.id)">Send</button>
</div>

And define the handler in methods section:

<script>
export default {
data: ...
methods: {
    sendItem: function(itemId) {
        // Using axios here
    }
}
}
</script>

Upvotes: 5

Related Questions