2017-04-20 50 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

最歡迎:) –