Component without template

前端 未结 2 984
眼角桃花
眼角桃花 2021-02-18 18:24

I have a bit of code that makes an api call to a server and returns some JSON.

It did exist as a method in my component but as it is getting a bit long I want to extrac

2条回答
  •  孤独总比滥情好
    2021-02-18 18:50

    Mixins work, or you could create a plugin. Here's the docs example:

    MyPlugin.install = function (Vue, options) {
      // 1. add global method or property
      Vue.myGlobalMethod = function () {
        // something logic ...
      }
    
      // 2. add a global asset
      Vue.directive('my-directive', {
        bind (el, binding, vnode, oldVnode) {
          // something logic ...
        }
        ...
      })
    
      // 3. inject some component options
      Vue.mixin({
        created: function () {
          // something logic ...
        }
        ...
      })
    
      // 4. add an instance method
      Vue.prototype.$myMethod = function (methodOptions) {
        // something logic ...
      }
    }
    

    Vue Plugins

提交回复
热议问题