重复跳转了同一个页面,导致空值台报错了!

解决思路:
方案1:在路由跳转时捕获错误。

1.1 全局捕获处理

//index.js
import VueRouter from 'vue-router'
import routes from './routes'

Vue.use(VueRouter)
const router = new VueRouter({
  routes
})

// 路由重复跳转时捕获错误信息 ===> 添加如下的代码
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

1.2 局部捕获处理

this.$router.push(route).catch(err => {
  console.log('输出报错',err)
})

方案2:代码里自己判断如果前后路由相同,不做跳转操作。

toNewPage (obj) {
  if (this.$route.path !== obj.url) {
    this.$router.push({ path: obj.url })
  }
}

参考网上资源:https://blog.csdn.net/qq_35393869/article/details/122881393

posted on 2022-07-22 16:50  ╰透在骨子里的小傲娇  阅读(240)  评论(0)    收藏  举报