美文网首页
VueUncaught (in promise) Navigat

VueUncaught (in promise) Navigat

作者: 天渺工作室 | 来源:发表于2021-05-10 21:48 被阅读0次

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

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

    或者

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

    废话不多说

    方法1:

    https://mp.csdn.net/editor/html/107813784

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

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

    方法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
    })
    
    ...
    
    image.gif image image.gif

    相关文章

      网友评论

          本文标题:VueUncaught (in promise) Navigat

          本文链接:https://www.haomeiwen.com/subject/fkrwdltx.html