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获取不到

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程宝库

需求是:输入框只能输入数字,包括整数和小数发现网上大部分文章的处理方式:通过正则来处理输入,对其合法性进行判断校验,再将提示信息反馈给用户。体验欠佳输入完成后进行解析,将字 ...