微信小程序 路由跳转 MiniProgramError {“errMsg“:“navigateTo:fail webview count limit exceed“} Object

微信小程序 跳转页面 页面栈层数限制,如果继续使用navigateTo会报错,所以统一处理,报错了就用redirectTo
/**
 * 跳转到下一页面
 * 1.支持只跳页面,不带参数
 * eg: jump('/pages/index/select-address/select-address')
 * 2.支持跳页面, 页面url 后面以"?"的方式带参数:
 * eg: jump('/pages/index/select-address/select-address?isEnd=true&abc=456')
 * 3.支持跳转页面,页面url 和 参数以对象的方式传入:
 * eg: jump({path: '/pages/index/select-address/select-address', params: {isEnd: true}})
 */
const jump = (to) => {
  let options = {};
  let url = '';
 
  if (typeof to === 'object') {
    url = to.path;
 
    if (to.params) {
      url = addParams(url, to.params);
    }
  } else if (typeof to === 'string') {
    let index = to.indexOf('?');
 
    if (index !== -1) {
      url = addParams(to.substring(0, index), getParams(to));
    } else {
      url = to;
    }
  }
 
  options = {
    url: url,
    fail: (res) => {
      console.log(res);
      if (res.errMsg === 'navigateTo:fail webview count limit exceed') {
        redirectTo(to);
      }
    }
  }
  let pages = getCurrentPages();
  let curPage = pages[pages.length - 1];
  if (options.url.substring(1) !== curPage.route) {
    wx.navigateTo(options);
  }
}
 
const back = (popIndex) => {
  let delta = 1;
 
  if (popIndex) {
    delta = popIndex;
  }
 
  wx.navigateBack({
    delta: delta
  });
}
 
/**
 * 关闭当前页面,跳转到应用内的某个页面
 * @param {*} to
 */
const redirectTo = (to) => {
  let options = {};
  let url = '';
 
  if (typeof to === 'object') {
    url = to.path;
 
    if (to.params) {
      url = addParams(url, to.params);
    }
  } else if (typeof to === 'string') {
    let index = to.indexOf('?');
 
    if (index !== -1) {
      url = addParams(to.substring(0, index), getParams(to));
    } else {
      url = to;
    }
  }
 
  options = {
    url: url
  };
  let pages = getCurrentPages();
  let curPage = pages[pages.length - 1];
  if (options.url.substring(1) !== curPage.route) {
    wx.redirectTo(options);
  }
}
 
const getParams = (url) => {
  let params = {};
 
  if (!url || !url.trim()) {
    return params;
  }
 
  if (url.indexOf('?') < 0) {
    return params;
  }
 
  let queryStr = url.substring(url.indexOf('?') + 1);
 
  if (queryStr.indexOf('&') < 0) {
    let map = queryStr.split('=');
    let key = map[0];
    params[key] = map[1];
  } else {
    queryStr.split('&').forEach((item) => {
      let map = item.split('=');
      let key = map[0];
      params[key] = map[1];
    })
  }
 
  return params;
}
 
const addParams = (url, params) => {
  let kvArr = [];
  for (let key in params) {
    if (key) {
      kvArr.push(key + '=' + params[key]);
    }
  }
 
  return url.indexOf('?') < 0 ? url + '?' + kvArr.join('&') : url + '&' + kvArr.join('&');
}
 
module.exports = {
  jump: jump,
  back: back,
  redirectTo: redirectTo
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值