Taro
、uni
、mpVue
等多端网络请求javascript
目的:实现各多端框架(
taro
、uniApp
、mpvue
等)统一请求代码。vue
基于 Promise 对象实现更简单的 request 使用方式,支持请求拦截
、响应拦截
和全局配置
,为项目迁移到别的框架实现解耦。欢迎为本项目提issus。java
# 使用 npm 安装
$ npm i request-core
# OR 使用 cnpm 安装
$ cnpm i request-core
# OR 使用 yarn 安装
$ yarn add request-core
复制代码
// app.js
import http from 'request-core/src/taro'
Taro.$http = http
//xxx.js
//Taro.$http(options={}) 或 Taro.$http.get(url,data,options={})
复制代码
//main
import http from 'request-core/src/uni'
Vue.prototype.$http = $http;
//xxx.vue
//this.$http(options={}) 或 this.$http.get(url,data,options={})
复制代码
首先,你须要将 node_modules 目录下的 request-core 拷贝到 项目根目录 或 须要的目录。node
//app.js
import http from './request-core/src/weapp'
wx.$http = http;
App({
....
})
//xxx.js
// wx.$http(options={}) 或 wx.$http.get(url,data,options={})
复制代码
可用的全局配置
baseURL
、url
、data
、header
、method
、dataType
和框架自带的其余配置,好比Taro h5 端的配置。git
http.config = {
dataType:'text',
baseURL:'http://localhost:3000',
header: {
"Content-Type": "awe5g45aewg45ewg1",
},
}
复制代码
http.interceptors.request.use(config=>{
console.log('请求拦截',config)
config.data.name = '测试'
return config
},err=>{
console.log('拦截请求报错',err)
})
http.interceptors.response.use((res)=>{
console.log('响应拦截',res)
return res.data
},(err)=>{
console.log('拦截响应报错',err)
})
复制代码
局部配置优先级高于全局配置,全局配置优先级高于默认配置。github
http({
url:'users/test3',
data:{
name:'111',
test:'222',
},
}).then(res=>{
console.log('请求结果',res)
}).catch(err=>{
console.log('报错了》》》》',err)
})
复制代码
可用的请求方式 有
OPTIONS
,GET
,HEAD
,POST
,PUT
,DELETE
,TRACE
,CONNECT
npm
http.get('users/test3',{
name:'111',
test:'222',
})
.then(res=>{
console.log('请求结果',res)
}).catch(err=>{
console.log('报错了》》》》',err)
})
复制代码