小程序中的axio——flyio的使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/marendu/article/details/98592838

flyio的使用

在小程序中使用请求,只能使用原生的wx.request,如果想要向axio一样使用三方包,只能使用flyio,不然会报错,同时flyio是属于多种兼容的可以放心使用到多端。

import Fly from 'flyio/dist/npm/wx'
const fly = new Fly()
const host = process.env.NODE_ENV === "development"? "模拟地址" : "真实地址"
fly.config.baseURL =  host
fly.config.headers = {
  "X-Tag": "flyio",
  'content-type': 'application/json',
  'Authorization': '',
  'sign': ""
} //设置你的请求头

// 添加请求拦截器
fly.interceptors.request.use((request) => {
  wx.showLoading({
    title: "加载中",
    mask: true
  });

  return request;
});

// 添加响应拦截器
fly.interceptors.response.use(
  (response) => {
    wx.hideLoading();
    return response;// 请求成功之后将返回值返回
  },
  (err) => {
    // 请求出错,根据返回状态码判断出错原因
    console.log(err);
    wx.hideLoading();
    if (err) {
      return "请求失败";
    };
  }
);

export default fly;

h官方网站 :https://wendux.github.io/dist/#/doc/flyio/readme

猜你喜欢

转载自blog.csdn.net/marendu/article/details/98592838
今日推荐