3

I have component

and i want run method after click

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

i have this method on main app methods

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

    data: {},

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

but seems like "click" not works on full component

1
  • It should work. Can you provide a codepen or jsfiddle Commented Apr 20, 2017 at 8:51

1 Answer 1

7
  1. Register your component, and declare the handler method inside:

     Vue.component('my-component', {
         // ...
         methods: {
             showOtherDiv : function(){
                 alert('Some message');
             }
         }
     });
    
  2. Add the .native modifier to the event name:

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

    From the docs:

    Binding Native Events to Components

    […] when you want to listen for a native event on the root element of a component […] you can use the .native modifier for v-on.

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

Comments

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.