1.0.0 • Published 4 years ago

dream-axios v1.0.0

Weekly downloads
6
License
ISC
Repository
github
Last release
4 years ago

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类

    参数

    参数名称类型说明
    environmentObject配置不同环境下根地址
    overtimeNumber配置默认请求超时

    environment参数列表

    参数名称类型说明
    environmentString开发环境的默认路径
    productionString生产环境的默认路径
    testString测试环境的默认路径

代码例子:

let dreamHttp = new DreamHttp({
    environment:{           //配置不同环境下根地址
     environment:'http://localhost:8999/api/', //开发环境
     production:'http://localhost:8999/api/',  //生产环境
     test:'http://localhost:8999/api/'         //测试
   },
   overtime:1000     //设置请求默认请求超时
})
  • dreamHttp实例对象方法
方法名称参数说明
requsetfunction*2配置请求拦截方法,参数是两个回调函数,1.默认是请求拦截成功的回调data,2.默认是请求拦截失败的回调err
responsefunction*2配置响应拦截方法,参数是两个回调函数,1.默认是响应拦截成功的回调data,2.默认是响应拦截失败的回调err
geturl , params设置get请求
posturl , params设置post请求
setApiPathObject设置api接口
setTokenObject设置令牌

代码例子:

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