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); }
// 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
axios
vue-resource
vue-axios
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
Then just import and use it in other files
Use
axios
. Officially, it is no longer recommended to usevue-resource
. Use axios together withvue-axios
to useRegister a plugin globally
https://vuefe.cn/v2/guide/plu...
Then use the file and you can use it