原因是内部promise是失败的,而我们又没有对错误进行处理
整写法应该是下面这个写法,不但要传递路由匹配地址或者对象,还要给部promise传递处理回调,如果一个都不传,那么内部失败的promise就没办法处理出Uncaught (in promise) NavigationDuplicated,如果要解决至少要传递一个调,传递了回调,失败的promise就有处理了
this.$router.push({
name:'search',
params:{keyword:this.keyword},
query:{keyword1:this.keyword.toUpperCase()}
() => {},() => {})
//() => {} 是给push内部promise传递的成功和失败的处理回调
方法1:降版本
方法2:处理失败的promise 在push调用后加.catch(()=>{})
,但是这种方法每次都要加,不方便
toSearch() {
this.$router.push({
name: "search",
params: { keyWord: this.keyWord || undefined },
query: { keyWord1: this.keyWord },
}).catch(()=>{});
},
方法3:封装源码,一劳永逸
import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter)
//this.$router.push其实调用的是VueRouter原型当中的push
//第一步:先把原本的push保存起来,后期还要用
const originPush=VueRouter.prototype.push
const originReplace=VueRouter.prototype.replace
//第二步:把原来的push方法重新赋值,封装
//后期再次调用this.$outer.push调用的就是自己封装的方法了
VueRouter.prototype.push=function(location,resolved,rejected){
if(!resolved&&!rejected){
//如果直接return originPush(),那么这个this不是路由对象而是window,所以使用call修改this为路由对象
return originPush.call(this,location).catch(()=>{})
}else{
return originPush.call(this,location,resolved,rejected)
}
}
//replace和push同理
export default new VueRouter({
})