Vue Router修改query参数url参数没有变化问题及解决

Router修改query参数url参数没有变化问题

正常情况下

this.$router.push({
query:{}
})
this.$router.replace({
query:{}
})

就可以修改了

但是当已有query对象里面修改其中一个值,就会发现虽然this.$route.query发生改变但是浏览器的url上的参数并没有发生变化, 尝试将已有的参数进行深拷贝突然发现就可以了

let newQuery= JSON.parse(JSON.stringify(this.$route.query));
newQuery.index = 2;
this.$router.replace({
query: newQuery
})

vueRouter不切换url只修改query报错

使用push的话  会导致返回历史有记录

this.$router.push({
  query: {
    id: this.processId
  }
})

所以需要使用

this.$router.replace({
  query: {
    id: this.processId
  }
})

虽然不影响使用,但是会报如下错误

解决方案

在router.js加上这段

import VueRouter from 'vue-router'
 
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {
 return originalReplace.call(this, location).catch(err => err)
} 
 
// push的同理
 
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
 return originalPush.call(this, location).catch(err => err)
}

注 适用于3.0.0版本的vue-router,   3.4.x可能会报错.catch获取不到

作者:艾路菲尔原文地址:https://blog.csdn.net/weixin_38566069/article/details/107523224

%s 个评论

要回复文章请先登录注册