A
A
Alexander Ivanov2018-03-05 13:16:07
Vue.js
Alexander Ivanov, 2018-03-05 13:16:07

How to get index from list in VUE?

<template>
    <div>
        <hr>
        <h1>{{ text }}</h1>
        <br>
        <ul v-model="list" :key="index">
            <li v-for="(item, index) in list" @click="testA" class="list btn">
{{ index }} {{ item.name }}
</li>
        </ul>
    </div>
</template>

<script>
export default {
    name: 'test',
    data(){
        return{
            text: "Текст",
            list: [
                { name: 'Petya'},
                { name: 'Vasia'},
                { name: 'Gosha'},
            ]
        }
    },
    methods: {
        testA(){
            alert(this.index)
            console.log(this)
            // alert(this.list[this.index].name)
        }
    }
}
</script>

I tried it in different ways, but so far I have not passed the key through testA

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-03-05
@cimonlebedev

Just pass the index to the handler:

testA(index) {
  alert(index);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question