javascript - How to Set A Methods For A Class In Vue.js - Stack Overflow

you know that we can use on events for a class in JQuery. for instance $(".example").click(f

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

David'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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745295040a4621088.html

相关推荐

  • javascript - How to Set A Methods For A Class In Vue.js - Stack Overflow

    you know that we can use on events for a class in JQuery. for instance $(".example").click(f

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信