3

HTML:

<ul>
    <item
      v-on:click="boom"
      v-bind:product="item"
      v-bind:key="item.id"
      v-for="item in items"
     ></item>
</ul>

Javascript:

Vue.component('item', {
    props: ['product'],
    template: '<li><a href="#">{{ product.name }}</a></li>',
});

let app = new Vue({
    el: 'ul',
    data: {
        items: [
            { id: 0, name: 'penda' },
            { id: 1, name: 'harla' },
            { id: 2, name: 'calar' },
        ],
    },
    methods: {
        boom: function (e) {
            alert('Aha!');
        }
    }
});

Everything is rendered fine, but the boom event listener is never fired. Do you know the reason why?

2 Answers 2

4

Use @click.native="boom" as @Pvl answered. OR bind the custom from parent component and emit from child. please run & check the below code.

Vue.component('item', {
    props: ['product'],
    template: '<li @click="clickevent($event)"><a href="#">{{ product.name }}</a></li>',
    methods:{
clickevent(e){
this.$emit('componentclick', e)
}
}
});

let app = new Vue({
    el: 'ul',
    data: {
        items: [
            { id: 0, name: 'penda' },
            { id: 1, name: 'harla' },
            { id: 2, name: 'calar' },
        ],
    },
    methods: {
        boom: function (e) {
            alert('Aha!');
            console.log(e)
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<ul>
    <item
      @componentclick="boom"
      v-bind:product="item"
      v-bind:key="item.id"
      v-for="item in items"
     ></item>
</ul>

Sign up to request clarification or add additional context in comments.

Comments

3

Try to add .native to get v-on:click.native="boom"

More details can be found here

4 Comments

My understanding is this: the root instance was trying to listen to an event from the child component. But, if the child component doesn't tell the root instance about it (via emit), the event listener will never be triggered. Does this mean that native events don't follow this pattern?
As I understand you can create custom click like $emit('click', e) and in this case, you will have the conflict between native and custom click events. To prevent situations like that '.native' modifier was introduced.
.native tells vue to add the listener for the child root node (li in your example). If you try to combine my code with the code from @dagalti's answer you can find two listeners for the click event on the same element. jsfiddle.net/fc0p9jq2
Thank you. I'm still confused as to why it's possible to access the parent's boom when using the native modifier. I guess this is to ensure nothing gets passed from child components without explicitly saying so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.