pass index to child event listener in parent component in vuejs

前端 未结 2 2088
粉色の甜心
粉色の甜心 2021-01-20 02:47

I have a list of objects within a v-for loop:

相关标签:
2条回答
  • 2021-01-20 02:56

    You can bind an arrow function expression in your event handler. For example

    <child @event-fired="dataFromChild => handleEvent(index, dataFromChild)"/>
    

    JSFiddle demo (from the Vue boilerplate) ~ https://jsfiddle.net/zmxksv35/

    0 讨论(0)
  • 2021-01-20 03:12

    Just pass everything into your event handler as a single object.

    <div v-for="(element, index) in myArray">
        <child @event-fired="data => handleEvent({ index, data })"></child>
    </div>
    

    Then, in your event handler, you can destructure it:

    handleEvent({ index, data }) {
        // handle the event
    }
    
    0 讨论(0)
提交回复
热议问题