...">

How to click on all vuejs component

I have a component

and I want to run the method after clicking

<my-component @click="showOtherDiv"></my-component>

      

I have this method for main application methods.

var app = new Vue({
    el: '#app',

    data: {},

    methods: {
        showOtherDiv : function(){
            alert('Some message');
        }
    }
});

      

but it looks like "click" doesn't work with full component

+3


source to share


1 answer


  • Register your component and declare a handler method inside:

    Vue.component('my-component', {
        // ...
        methods: {
            showOtherDiv : function(){
                alert('Some message');
            }
        }
    });
    
          

  • Add a modifier .native

    to the event name:

    <my-component @click.native="showOtherDiv"></my-component>
    
          

    From docs :

    Binding Native Events to Components

    [& hellip;] when you want to listen to the native event on the root element of the [& hellip;] component, you can use the modifier .native

    for v-on

    .



+7


source







All Articles