如何单击整个 vuejs 组件 [英] How to click on whole vuejs component

查看:24
本文介绍了如何单击整个 vuejs 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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. 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.

这篇关于如何单击整个 vuejs 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆