Is it possible to nest methods in Vue.js in order to group related methods?

后端 未结 5 704
礼貌的吻别
礼貌的吻别 2021-02-06 06:33

I\'d like to group some of my Vue.js methods together in a sort of \"submethod\" class, but I only seem to be able to have single level methods.

For example, if I wanted

5条回答
  •  孤独总比滥情好
    2021-02-06 07:15

    if i had that problem, i would use a click handler() to delegate request to other methods. eg:

    new Vue({
    
        el: '#app',
    
        data: { },
    
        methods: {
    
            handler1: function() {
                 console.log("handler 1 called");
            },
    
            handler2: function() {
                console.log("handler 2 called");
            },
    
            buttonHandler:function(callback){
                callback();
            }
    
    
        }
    
    });
    

    and use html as

    
    
    
    

    The code is only for demo. In real life i will be passing a number or string argument in template and using switch case to determine handler.

提交回复
热议问题