javascript - How to re-encapsulate vue-resource
淡淡烟草味
淡淡烟草味 2017-07-05 10:58:37
0
2
1071

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);
}
淡淡烟草味
淡淡烟草味

reply all(2)
大家讲道理
// 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

仅有的幸福

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!