全局使用 (推荐)
局部使用
  1. const res = await req.ajax({
  2. path: "https://www.easy-mock.com/mock/5ca6ec41215a7b66ff10343d/example/query",
  3. })
1.简单使用
  1. const res = await this.$req.ajax({
  2. path: "example/query", //可以是完整路径也可以后半部分
  3. })
  4. console.log(res);
2.带请求提示
  1. const res = await this.$req.ajax({
  2. path: "example/query",
  3. title: "正在加载", //false 则不显示
  4. })
  5. console.log(res);
3.带参数请求
4.单个请求带拦截
  1. const res = await this.$req.ajax({
  2. path: "example/query",
  3. title: "正在加载",
  4. data:{
  5. name:'hhyang'
  6. },
  7. abortFun: (info,bt) => {
  8. bt.abort();
  9. },
  10. })
  11. console.log(res);
5.监听请求完成,无论失败还是成功。当然可以使用 then、catch、finally 详细
  1. const res = await this.$req.ajax({
  2. path: "example/query",
  3. data:{
  4. name:'hhyang'
  5. },
  6. finishFun:finsh => { //使用await 用此方法即可监听到
  7. console.log(finsh)
  8. }
  9. })
  10. console.log(res); //成功之后的返回,如要捕捉失败请使用try、catch
6.携带额外参数,不会上传。可提供给开发者分辨某个请求 详细
  1. const res = await this.$req.ajax({
  2. path: "example/query",
  3. title: "正在加载",
  4. data:{
  5. name:'hhyang'
  6. },'我是额外参数1''我是额外参数2')
  7. console.log(res); //成功之后的返回,如要捕捉失败请使用try、catch
  8. //或者这样
  9. const res = await this.$req.ajax({
  10. path: "example/query",
  11. title: "正在加载",
  12. data:{
  13. name:'hhyang'
  14. },
  15. },{
  16. parmas1:'我是额外参数1',
  17. parmas2:'我是额外参数2',
  18. })
  19. console.log(res); //成功之后的返回,如要捕捉失败请使用try、catch
7.捕捉请求错误
8.自定义 header、dataType、responseType、type
  1. const res = await this.$req.ajax({
  2. path:"example/query",
  3. type:"POST",
  4. header:{
  5. 'content-type': 'application/x-www-form-urlencoded'
  6. },
  7. dataType: 'json',
  8. responseType: 'text',
  9. })
  10. console.log(res);
9.设置全局请求前拦截
  1. //延迟函数
  2. let timeout=function(time=3000){
  3. return new Promise(resolve=>{
  4. setTimeout(()=>{
  5. resolve();
  6. },time)
  7. })
  8. //同步写法
  9. req.defaultReq.beforeSend=res=>{
  10. if(res.data.token!=''){ //验证token存在的情况下才放行
  11. return res
  12. }
  13. }
  14. //同步写法
  15. req.defaultReq.beforeSend=res=>{
  16. delete res.data; //删除传递的参数选项
  17. return res; //返回新的请求参数
  18. }
  19. //异步写法
  20. req.defaultReq.beforeSend=async res=>{
  21. await timeout(); //3秒后执行删除,再返回
  22. delete res.data; //删除传递的参数选项
  23. return res; //返回新的请求参数
  24. }
10.设置全局响应后拦截
  1. //延迟函数
  2. let timeout=function(time=3000){
  3. return new Promise(resolve=>{
  4. setTimeout(()=>{
  5. resolve();
  6. },time)
  7. })
  8. }
  9. //同步写法
  10. req.defaultReq.beforeFinish=res=>{
  11. if(res.data.userOut){ //退出登录不返回数据,标记此次请求无效
  12. uni.reLaunch({
  13. url: 'login?userOut=true'
  14. });
  15. }else{
  16. return res;
  17. }
  18. }
  19. //同步写法
  20. req.defaultReq.beforeFinish=res=>{
  21. return {msg:'这是我自定义的数据'}; //返回新的响应数据
  22. }
  23. //异步写法
  24. req.defaultReq.beforeFinish=async res=>{
  25. await timeout(); //3秒后执行
  26. return {msg:'这是我自定义的数据'}; //返回新的响应数据
  27. }
11.设置全局错误拦截

全局 defaultReq 设置完成后针对全局有效,ajax 方法默认在你没传值的情况下优先采用的全局参数。可以通过向 ajax 方法传递参数覆盖全局的详细