vue路由跳转相同页面多次报错NavigationDuplicated
报错不影响程序运行,但是爆红就很烦人,需要去解决
报错原因是vue-router组件使用过程中,this.$router.push返回的是Promise
就好比
function push(){
return new Promise((resolve,reject)=>{
})
}
解决一:捕获成功值,错误值(治标不治本)
this.$router.push({
name: "project",
params: { projectPattern: this.projectPattern },
},()=>{},()=>{ });
解决二: 重写push
使用还是与以前一样
this.$router.push({
name: "project",
params: { projectPattern: this.projectPattern },
});
在router/index.js文件下,通过判断有无resolve、reject传入值,写一个处理
let originPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location, resolve, reject) {
if (resolve && reject) {
originPush.call(this,location,resolve,reject)
}else{
originPush.call(this,location,()=>{},()=>{})
}
}```