l***@hotmail.com
l***@hotmail.com
  • 发布:2019-05-11 18:12
  • 更新:2019-05-11 21:03
  • 阅读:1892

#插件讨论# 【 仿axios封装request网络请求库,支持拦截器全局配置 - webwork.s@qq.com 】 怎么配置

分类:uni-app
  1. 没有utils目录就建立一个吗
    2 在main.js中需要设置吧? 你文档中没写清楚怎么引入这个插件。

    我在main.js 这样引入这个插件的

    import request from '@/utils/http-request/index.js'  
    ...  
    Vue.prototype.$request=request

    然后在使用页面

    this.request.post('/user/login', {username: 'xxx',passsword:'xxxx'}, {  
     header: {}, /* 会覆盖全局header */  
     dataType: 'json',  
     responseType: 'text'  
    }).then(res => {  
    
    }).catch(err => {  
    
    })

结果在微信开发者调试的network中发现了test和http两个请求
{test: Request, http: Request}

自己改的baseUrl也没用,里面的config的url还是 "http://www.bbb.cn"

2019-05-11 18:12 负责人:无 分享
已邀请:
luch

luch

1:关于存放文件夹目录可以自定义存放。我是存放在 /utils/http-request
2: 引用方法(1)全局引入,在index文件里面我已经new 了俩个实例,一个test,一个http。 例如我想把http全局引入。在main.js引入import {http} from '@/utils/http-request/index.js'; Vue.prototype.$request=http; 然后可以全局使用this.$request.post().then().catch()等等方法,关于http这个函数的一些全局配置,我在index.js文件里面 调用了http.setConfig()方法,在里面修改配置即可。例如:

  export const http = new Request();  
        http.setConfig((config) => { /* 设置全局配置 config 为默认全局配置*/  
            config.baseUrl = '这里放置你的根路径';   
            config.header = {  
                a: 1,  
                b: 2  
            }  
            return config  
        })

关于全局可设置参数可以阅读里面的Readme文件;
(2)局部引入:例如test这个请求配置,我想在部分文件使用。可以在要使用的文件里先import {test} from '@/utils/http-request/index.js';引入;然后 可使用test.post().then().catch()等等方法;
需要注意的是修改全局配置是需要调用setConfig 方法。
欢迎提问和提建议...

该问题目前已经被锁定, 无法添加新回复