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

后端 未结 5 693
礼貌的吻别
礼貌的吻别 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 06:55

    I had the same issue (the need of a namespace) while writing a Vue mixin. This answer doesn't directly address your case, but it could give a clue.

    This is how I defined the mixin.

    export default {
       created () {
        // How to call the "nested" method
        this.dummy('init')
    
        // Pass arguments
        this.dummy('greet', {'name': 'John'})
       },
    
       // Namespaced methods
       methods: {
         dummy (name, conf) {
           // you can access reactive data via `that` reference,
           // from inside your functions
           const that = this
    
           return {
             'init': function (conf) {
                console.log('dummy plugin init OK')
             },
             'greet': function (conf) {
                console.log('hello, ' + conf['name'])
             }
           }[name](conf)
         }
       }
     }
    

    PS: for an official solution, Evan You said no.

提交回复
热议问题