vue解决因添加全局路由参数导致的重复进入路由的问题

如题,由于场景需要,所以在全局前置守卫beforeEach里面加了公用参数

if (!to.query.source || !to.query.saleChannelId) {
  const query = {
    ...to.query,
    source: window.localStorage.getItem('_source'),
    saleChannelId: window.localStorage.getItem('_saleChannelId'),
  };
  next &&
    next({
      path: to.path,
      query: query,
    });
} else {
  next && next();
}

但是也引发一个问题,就是同一个页面会进入两次,虽然我在use(router)前加了如下代码解决了报错提示,但是不解决重复进入的问题

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};
阅读 3.3k
3 个回答

next && next();
前面的next是干嘛的?可能是它导致的

if else 整个替换成下面的试试
next({
path:to.path,
source:to.query.source?to.query.source:localstorage.getItem('_source'),
saleChannelId:to.query.saleChannelId?to.query.saleChannelId:window.localStorage.getItem('_saleChannelId')
})

推荐问题