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);
}
大家讲道理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
仅有的幸福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