本文已参与「新人创作礼」活动,一起开启掘金创作之路。
解决vue-router重复点击报错问题
报错的原因很简单,vue-router 升级到 3.1.x 后,重复点击导航时,控制台出现报错 ,vue router ≥ v3.1 后 ,回调形式改成 promise api 了,返回的是 promise,如果没有捕获到错误,控制台始终会出现警告。
解决办法一:
在index.js路由配置文件
import Vue from 'vue'
import Router from 'vue-router'
//解决vue-router重复点击报错问题(this.$router.replace())
const originalPush = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
return originalPush.call(this, location).catch(err => err);
};
//解决vue-router重复点击报错问题(this.$router.push())
const originalPush = Router.prototype.push;
Router.prototype.push= function push(location) {
return originalPush.call(this, location).catch(err => err);
};
注意区分this.router.replace()和this.router.push()
解决办法二:
找到跳转引起报错的地方,加catch, 这个console.log可以啥也不打印,但是必须得写,否则无效
router.replace('/').catch(err => { console.log(); })