在VueRouter

在使用Vue开发中,经常会遇到 vue-router报错Uncaught (in promise),这是一个警告,虽然不影响代码编写,但是作为一个要求严谨的高级屌丝,必须解决这个问题。
造成这个问题的主要原因是,路由发现已经在当前的路由地址,还继续给这个路由地址跳转,就给出警告。
在这里插入图片描述
解决办法,在路由的js文件中添加如下代码即可

import Vue from 'vue'
import VueRouter from 'vue-router'

const originalPush = VueRouter .prototype.push
VueRouter .prototype.push = function push(location, onResolve, onReject) {
    
    
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

猜你喜欢

转载自blog.csdn.net/baozi7263/article/details/128738350