2017-04-20 59 views
2

我有分量如何点击整个vuejs组件

,我想点击后运行方法

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

我对主应用程序的方法

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

    data: {}, 

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

这种方法但似乎像“点击”不适用于全部组件

+0

它应该工作。你能提供一个codepen或jsfiddle – CodinCat

回答

7
  1. 注册您的组件,并声明里面的处理方法:

    Vue.component('my-component', { 
        // ... 
        methods: { 
         showOtherDiv : function(){ 
          alert('Some message'); 
         } 
        } 
    }); 
    
  2. .native改性剂添加到事件名称:

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

    the docs

    将本地事件绑定到组件

    [&hellip;]当您想要监听组件的根元素上的本地事件[&hellip;]可以使用.native改性剂v-on

+1

谢谢你的作品 – Fatas

+1

最欢迎:) –