在router的index.js中的路径中,对push跳转方法进行重写。
里面的vueRouter是vue路由对象,对应该页面引入的vueRouter
import VueRouter from "vue-router";
//push路由跳转方法的重写,同时对重复跳转同一路由错误做处理
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}
写在router的index.js对所有路由生效;写在某个vue中,对单个有效
执行的思路,
1,用面向对象的原型的方式,,写了一个push的方法,
2,点击一次调用一次函数,下一次点击就近原则选择最近的函数
3,call是继承,执行一次继承之前的方法(this,),又做了一次缓存catch(err => err)错误处理;回调函数vue的箭头函数封装
4,location,跳转的网址