Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic component click event in Vue

I render Vue components using v-for:

<component
    v-for="component in components"
    :is="component.type"
    @click="myFunction(component.id)">
</component>

Clicking on the rendered component doesn't fire the myFunction method. However, clicking a manually inserted component does:

<div @click="myFunction('...')"></div>

How to correct this?

like image 938
Mikko Avatar asked Dec 12 '16 09:12

Mikko


1 Answers

Add the .native modifier to listen for a native event instead of a component event.

<component
    v-for="component in components"
    :is="component.type"
    @click.native="myFunction(component.id)">
</component>

Source: https://vuejs.org/v2/api/#v-on

like image 99
bzeghers Avatar answered Sep 21 '22 08:09

bzeghers