如何点击整个vuejs组件
问题描述:
我有分量如何点击整个vuejs组件
,我想点击后运行方法
<my-component @click="showOtherDiv"></my-component>
我对主应用程序的方法
var app = new Vue({
el: '#app',
data: {},
methods: {
showOtherDiv : function(){
alert('Some message');
}
}
});
这种方法但似乎像“点击”不适用于全部组件
答
-
注册您的组件,并声明里面的处理方法:
Vue.component('my-component', { // ... methods: { showOtherDiv : function(){ alert('Some message'); } } });
-
的
.native
改性剂添加到事件名称:<my-component @click.native="showOtherDiv"></my-component>
从the docs:
将本地事件绑定到组件
[&hellip;]当您想要监听组件的根元素上的本地事件[&hellip;]可以使用
.native
改性剂v-on
。
+1
谢谢你的作品 – Fatas
+1
最欢迎:) –
它应该工作。你能提供一个codepen或jsfiddle – CodinCat