admin管理员组文章数量:1418667
you know that we can use on events for a class in JQuery. for instance
$(".example").click(function(){
//the process
})
I am new on Vue.js and I am working on methods in vue.js Vue use v-on attr to set a method for a element. But I dont want to define attr for all elements whichs use same function.
For Example
<ul class="container">
<li v-on="click:Menu" >Menu 1</li>
</ul>
<ol class="click">
<li v-on="click:Menu" >Menu 1</li>
<li v-on="click:Menu" >Menu 2</li>
</ol>
You must saw, I used v-on attr for all li elements. For li elements it is not problem, I can solve it v-repeat but for some cases, I have to set same function for lots of divs or form. I want to set a class for a function and set a method for the class.
Is there any solution for it?
you know that we can use on events for a class in JQuery. for instance
$(".example").click(function(){
//the process
})
I am new on Vue.js and I am working on methods in vue.js Vue use v-on attr to set a method for a element. But I dont want to define attr for all elements whichs use same function.
For Example
<ul class="container">
<li v-on="click:Menu" >Menu 1</li>
</ul>
<ol class="click">
<li v-on="click:Menu" >Menu 1</li>
<li v-on="click:Menu" >Menu 2</li>
</ol>
You must saw, I used v-on attr for all li elements. For li elements it is not problem, I can solve it v-repeat but for some cases, I have to set same function for lots of divs or form. I want to set a class for a function and set a method for the class.
Is there any solution for it?
Share Improve this question asked Jul 22, 2015 at 21:47 Kamuran SönecekKamuran Sönecek 3,3322 gold badges36 silver badges59 bronze badges2 Answers
Reset to default 4David's answer is good. But if you don't want to use Jquery, you can use document.querySelectorAll instead of $(this.el).find("li") and then add the click handlers with addEventListener in the directive.
Having said that, you don't have to add event listeners to all the elements in a directive (even though a directive might be the best solution for this). Another approach would be to do what you suggest, put the handlers on the parent elements, and then implement some logic depending on which element the function was called from. Example:
https://jsfiddle/q7xcbuxd/33/
<div id="app">
<ul class="UL_items" v-on="click:Menu(1)">
<li>Menu 1 item 1</li>
</ul>
<ol class="OL_items" v-on="click:Menu(2)">
<li>Menu 2 item 1</li>
<li>Menu 2 item 2</li>
<li>Menu 2 item 3</li>
</ol>
<div class="DIV_items" v-on="click:Menu(3)">
lots<br/>
of<br/>
items<br/>
</div>
</div>
var vue = new Vue({
el: '#app',
methods: {
Menu: function(id){
console.log('You invoked function Menu' + id + ' ' +
'by clicking on ' + event.path[0].innerHTML + ' ' +
'which is of type ' + event.srcElement.nodeName + '.\n' +
'The parent element\'s class name is \'' + event.srcElement.parentElement.className + '\' ' +
'which is of type ' + event.srcElement.parentElement.nodeName + '.');
}
}
});
I would write a directive for <ol>
and <ul>
that adds click handlers for all <li>
children.
Something like:
Vue.directive('menu', {
bind: function () {
$(this.el).find("li").on("click", function (event) {
this.menu_click(event);
});
},
update: function (value) {
this.menu_click = value;
},
unbind: function () {
$(this.el).find("li").off("click", this.menu_click);
}
})
And use it like this:
<ul class="container" v-menu="container_click">
<li>Menu 1</li>
</ul>
<ol class="click" v-menu="click">
<li>Menu 1</li>
<li>Menu 2</li>
</ol>
本文标签: javascriptHow to Set A Methods For A Class In VuejsStack Overflow
版权声明:本文标题:javascript - How to Set A Methods For A Class In Vue.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745295040a2652024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论