I have a set of data that needs to be looped through but each item in that data has a different onClick event. Something along the lines like:
list: [
{
text: 'a',
icon: 'a-icon',
onClick: () => {
// do stuff a
},
},
{
text: '`b`',
icon: 'b-icon',
onClick: () => {
// do stuff b
},
},
]
what I initially tried to do was:
<div v-for="(item, i) in list" :key="i" @click="item.onClick()"></div>
which didn't work. The error was:
TypeError: item.onClick is not a function
Then I tried:
<div v-for="(item, i) in list" :key="i" @click="list[i].onClick()"></div>
which looks weird but works. Only I can't access this inside onClick function. I could just pass this to the function directly like @click="list[i].onClick(this)" but it look weird too.
I knonw I can write a method and do switch according to the index. But then I had to write the onClick function seperately.
Is there a better way to do this?