vue-router中重写push方法

文章目的:

看到一段代码不理解什么意思,查了一下是为了解决,重复跳转相同的路由,控制台报错。 重写了方法后,能catch异常,不会在控制台报错了。

代码:

Router.prototype.push = function push(location) {
    console.log(location, 'location')
    return routerPush.call(this, location).catch(error => error)
}

解释:

写完路由重复点击路由两次,控制台上就会报错:如上图所示

解决它:代码如下

点击跳转同一个路径
在VueRouter上配置路由跳转,在router中的index.js中加上以下代码,注意加在use之前

import Vue from 'vue'
import VueRouter from 'vue-router'
 
// 解决路由重复跳转错误
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
    return routerPush.call(this, location).catch(err => { })
};
 
Vue.use(VueRouter)
posted @   崛起崛起  阅读(408)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2022-11-07 封装去重的方法
2022-11-07 markdown语法
2022-11-07 vue3学习文档
点击右上角即可分享
微信分享提示