README
Dream-http
说明
dream-http是基于axios进行二次封装,简单化网络请求
快速上手
import Vue from 'vue' import App from './App.vue' import router from './router' //1.导入dream-http包 import DreamHttp from 'dream-axios' Vue.config.productionTip = false new Vue({ router, render: h => h(App) }).$mount('#app')
new DreamHttp类
参数
参数名称 类型 说明 environment Object 配置不同环境下根地址 overtime Number 配置默认请求超时 environment参数列表
参数名称 类型 说明 environment String 开发环境的默认路径 production String 生产环境的默认路径 test String 测试环境的默认路径
代码例子:
let dreamHttp = new DreamHttp({
environment:{ //配置不同环境下根地址
environment:'http://localhost:8999/api/', //开发环境
production:'http://localhost:8999/api/', //生产环境
test:'http://localhost:8999/api/' //测试
},
overtime:1000 //设置请求默认请求超时
})
- dreamHttp实例对象方法
方法名称 | 参数 | 说明 |
---|---|---|
requset | function*2 | 配置请求拦截方法,参数是两个回调函数,1.默认是请求拦截成功的回调data,2.默认是请求拦截失败的回调err |
response | function*2 | 配置响应拦截方法,参数是两个回调函数,1.默认是响应拦截成功的回调data,2.默认是响应拦截失败的回调err |
get | url , params | 设置get请求 |
post | url , params | 设置post请求 |
setApiPath | Object | 设置api接口 |
setToken | Object | 设置令牌 |
代码例子:
let dreamHttp = new DreamHttp({
environment:{ //配置不同环境下根地址
environment:'http://localhost:8999/api/', //开发环境
production:'http://localhost:8999/api/', //生产环境
test:'http://localhost:8999/api/' //测试
},
overtime:1000 //设置请求默认请求超时
})
//requset()配置网络请求拦截
dreamHttp.requset(
function(data){ //data为请求拦截成功的回调参数
},
function(err){ //err为请求拦截成功的回调参数
})
//response()配置响应拦截
dreamHttp.response(
function(data){ //data为响应拦截成功的回调参数
},
function(err){ //err为响应拦截成功的回调参数
})
//get()网络请求
let id = 10001
dreamHttp.get('/login?id='+id).then(res=>{
console.log(res)
})
//或者
dreamHttp.get('/login',{id:id}).then(res=>{
console.log(res)
})
//post()网络请求
dreamHttp.post('/login',{id:id}).then(res=>{
console.log(res)
})
//设置请求头令牌,参数是一个对象,对象里面需要tokenName,tokenPrice参数
dreamHttp.setToken({
tokenName:'token',
tokenPrice:'shadasgysdfgasfdgsfdgfasgdf'
})
//setApiPath()配置每一个接口路径,然后在get,post请求可以直接调用接口api
dreamHttp.setApiPath({
login:'/login',
register:'/register'
})
let api = dreamHttp.api
//打印出来api是
//{
// login:'/login',
// register:'/register'
//}
dreamHttp.get(api.login,{id:id}).then(res=>{
console.log(res)
})
报错反应邮箱:3148264655@qq.com