How to click on whole vuejs component

限于喜欢 提交于 2019-12-02 01:23:12

问题


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:


  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.



来源:https://stackoverflow.com/questions/43514549/how-to-click-on-whole-vuejs-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!