vue2 router路由重复触发导致的报错

VueUncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/

 或者

vue Uncaught Error: Redirected when going from “/*“ to “/*“

废话不多说

方法1:

CSDN

这种解决方法可以是可以,但是方法2觉得更好些

 this.$router.replace({
        path: this.$route.path,
        query
   })
 .catch(()=>{});//把error 抛出来

方法2:

建议仔细看看github上这个讨论方案 说的很详细  https://github.com/vuejs/vue-router/issues/2881


//初始化 注入路由地方
import Vue from 'vue'
import Router from 'vue-router'

const originalPush = Router.prototype.push;
Router.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(Router)

...
const router = new Router({
  routes
})

...

Logo

前往低代码交流专区

更多推荐