基于 axios
的 http
请求库
npm install star-http-client -S
// src/api/index.js
import HttpClient from 'star-http-client'
class YourApiClient extends HttpClient {
constructor () {
super(YourAxiosInstance)
this.foo = this.foo.bind(this)
this.bar = this.bar.bind(this)
}
foo () {
return this.get('path/to/api')
}
bar (data) {
return this.post('path/to/api', data)
}
static install (Vue) {
let _this = new YourApiClient()
HttpClient.install(Vue, '$YourApiClient', _this)
}
}
export default YourApiClient
// main.js
const conf = {
ApiClient: YourApiClient // 可以传入一个数组 包含多个 ApiClient 对象 e.g. [ YourApiClient1, YourApiClient2, YourApiClient3 ]
}
this.$YourApiClient.foo() // 返回值为一个 Promise