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

后端 未结 5 695
礼貌的吻别
礼貌的吻别 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:07

    The closest I've got to doing this, is to declare the parent as a function, and return an object with a set of methods.

    Example:

    new Vue({
    
      el: '#app',
    
      data: {},
    
      methods: {
    
        buttonHandlers: function() {
          var self = this; // so you can access the Vue instance below.
    
          return {
    
            handler1: function() {
              dosomething;
              self.doSomething();
            },
    
            handler2: function() {
              dosomething;
            },
    
          },
    
        }
    
      }
    
    });
    

    And you can call the methods like this:

    
    

提交回复
热议问题