wxios 接口请求
针对 wx.request 接口进行二次封装。
- 使用
promise
调用风格 - 加入 拦截器功能
API
-
wxios(config)
; -
wxios(url[, config])
; -
请求 别名
wxios.request(config)
wxios.get(url, data[, config])
wxios.post(url, data[, config])
wxios.delete(url, data[, config])
wxios.head(url, data[, config])
wxios.put(url, data[, config])
-
wxios.all(promises)
var p1 = Promise;var p2 = Promise;wxiosallp1 p2; -
wxios.create([defaultConfig])
通过
wxios.create([defaultConfig])
创建一个全新的 wxios实例。var instance = wxios;新实例拥有 wxios下挂载的所有request方法。
-
请求拦截器
request拦截器 :
wxios.interceptors.request
// 添加一个拦截器// 同时返回一个 拦截器标识var interceptorId = wxiosinterceptorsrequest;// 删除一个拦截器wxiosinterceptorsrequest;response拦截器 :
wxios.interceptors.response
// 添加一个拦截器// 同时返回一个 拦截器标识var interceptorId = wxiosinterceptorsresponse;// 删除一个拦截器wxiosinterceptorsresponse; -
Default Config
// 公共接口前缀baseURL: ''// 公共 headers 配置headers: {}// 接口超时时间, 单位 ms, 默认为 0,不做超时处理timeout: 0wxiosdefaultsbaseURL = 'http://localhost';wxiosdefaultsheaderscommon'content-type' = 'application/json';wxiosdefaultheaderspost'content-type' = 'application/json'; -
取消请求
wxios.CancelToken
var cancelToken = wxiosCancelTokensource;wxios;// cancelcancelToken;