0.3.1 • Published 4 years ago

@xizhe/http-client v0.3.1

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

@xizhe/http-client

基于 axioshttp 请求库

一、安装(Install)

npm install @xizhe/http-client -S

二、使用(Usage)

1. 实例化
// src/api/index.js

import HttpClient from '@xizhe/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
2.在父框架配置中传入
// main.js
const conf = {
  ApiClient: YourApiClient // 可以传入一个数组 包含多个 ApiClient 对象 e.g. [ YourApiClient1, YourApiClient2, YourApiClient3 ]
}
3.使用
this.$YourApiClient.foo() // 返回值为一个 Promise