Home  >  Q&A  >  body text

javascript - How to re-encapsulate vue-resource

How about encapsulating vue-resource into a js file again, such as:

let Ajax = {
    Vue.http.get(url,data).then(
        // ...代码
        return data
    )
}

Then call it directly elsewhere, such as:

save(){
    this.Ajax.get(url,data);
}
淡淡烟草味淡淡烟草味2663 days ago1075

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-07-05 11:00:37

    // api.js
    
    export default {
        save (params = {}) {
          return Vue.http.get(url, { params }).then(res => {
            // some handling
            return res.data
          })
        },
        
        // ...
    }

    Then just import and use it in other files

    import api from './api'
    
    api.save({
      // params...
    }).then(data => {
      // ...
    })

    Use axios. Officially, it is no longer recommended to use vue-resource. Use axios together with vue-axios to use

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-07-05 11:00:37

    Register a plugin globally

    https://vuefe.cn/v2/guide/plu...

    export default {
      install: function() {
        Vue.prototype.$ajax = Ajax;
      }
    }

    Then use the file and you can use it

    reply
    0
  • Cancelreply