小程序报错errMsg":"hideLoading:fail:toast can't be found"和hideToast:fail:toast can't be found?

hideloading( )和toast的问题,不能多个loading和toast同时触发,下面是解决方案
我是封装的loading 解决思路一样,就是定义变量判断是否已经触发,避免多次触发
下面定义了一个变量来判断是否执行了loading

const host = "localhost";
export {
  host
};
//修改一
let _loading = false //定义一个变量来判断是否loading
// 请求封装s
export function request(url, data, method, header) {
    //修改二
    //判断有无loading 没有执行
  if(!_loading){
      wx.showLoading({
    title: "加载中" // 数据请求前loading
  });
  _loading = true
  }

  var date = new Date().getTime();
  let key;
  if (wx.getStorageSync("USER_INFO")) {
    key = JSON.parse(wx.getStorageSync("USER_INFO")).token;
  } else {
    key = "";
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: host + url, // 仅为示例,并非真实的接口地址
      method: method || "POST",
      data: data,
      header: {
        Authorization: key,
        "content-type": header || "application/json", // 默认值
        currenttime: date
      },
      success: function (res) {
     
        
        resolve(res.data);
      },

      fail: function (error) {
        Tips.error(error.msg);
        reject(error.msg);
      },
      
      complete: function (res) {
      //修改三, 存在loading再执行
        if(_loading){
          if (!res.data.success) {
            Tips.error(res.data.msg);
          } else{
            wx.hideLoading();
          }
          _loading = false
        }
      }
    });
  });
}
export function get(url, data) {
  return request(url, data, "GET");
}
export function post(url, data) {
  return request(url, data, "POST");
}

你可能感兴趣的:(小程序报错errMsg":"hideLoading:fail:toast can't be found"和hideToast:fail:toast can't be found?)