其他分享
首页 > 其他分享> > vue router报错 NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDu

vue router报错 NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDu

作者:互联网

其原因在于Vue-router在3.1之后把$router.push()方法改为了Promise。所以假如没有回调函数,错误信息就会交给全局的路由错误处理,因此就会报上述的错误。

禁止全局路由错误处理打印,这个也是vue-router开发者给出的解决方案:

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

把这段代码放在引入vue-router之后,一般在main.js里,如果你的路由单独抽取出来了,就放在路由文件中。

import Vue from "vue";
import Router from "vue-router";

//Vue-router在3.1之后把$router.push()方法改为了Promise。所以假如没有回调函数,错误信息就会交给全局的路由错误处理,因此就会报上述的错误。
//解决方案:禁止全局路由错误处理打印,这个也是vue-router开发者给出的解决方案
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router);

  

标签:vue,name,NavigationDuplicated,报错,Router,push,router,错误处理,路由
来源: https://www.cnblogs.com/dakai/p/12744703.html