Skip to content

console warnin:Duplicate named routes definition #1727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sailengsi opened this issue Sep 9, 2017 · 25 comments
Closed

console warnin:Duplicate named routes definition #1727

sailengsi opened this issue Sep 9, 2017 · 25 comments

Comments

@sailengsi
Copy link

我现在有一个这样的场景:
1,默认路由数组只有两个 ,根和登录。
2,登录之后,我会进行动态添加路由,实现方法如下:
this.$router.options.routes = new_routes;
this.$router.addRoutes(this.$router.options.routes);
3,然后退出的时候,再把动态添加的路由干掉,实现方式和上面类似:
this.$router.options.routes = new_routes;//new_routes确定只有两个
this.$router.addRoutes(this.$router.options.routes);
console.log(this.$router.options.routes);//这里也确定只有两个
此时此刻一切都正常,没有任何问题,然而
当我在登录时,重复第二步的操作的时候,会出现以下一大堆警告:

[vue-router] Duplicate named routes definition: { name: "日期时间", path: "/components/form/datetime" }
......
......
凡是在第一次动态添加过的路由,二次动态添加的时候,全部都会出现这些,但功能逻辑操作上,一切都没有问题。

不知道什么原因?或者有没有办法不让显示这个警告,尝试使用
Vue.config.productionTip = false;
Vue.config.devtools = false;
上面这两种方式关闭警告,但对vue-router不好使,查看源码之后,发现开发模式必须会警告,求解决办法。

如果方便,请回复中文,谢谢。

@vue-bot
Copy link

vue-bot commented Sep 9, 2017

Hello, your issue has been closed because it does not conform to our issue requirements. Please use the Issue Helper to create an issue - thank you!

@vue-bot vue-bot closed this as completed Sep 9, 2017
@lianghuilin
Copy link

遇到同样的问题

@bnuephjx
Copy link

bnuephjx commented Feb 3, 2018

遇到同样的问题,请问解决了么

@wangxdmm
Copy link

wangxdmm commented Feb 4, 2018

是不是 oldNameMap 缓存在内存中,当你再次登录的时候,如果没有刷新页面,会去比对你之前添加的oldNameMap, 但是你登录的时候要是刷新页面就不会有问题,有可能是这个问题。

@MrZhouZh
Copy link

name 同名了导致

@cat-kun
Copy link

cat-kun commented Sep 6, 2018

@shapl
在vue-router中定义了相同的name,刚解决。http://www.xiaoyulive.top/m/?post=124 打开后404

@vxhly
Copy link

vxhly commented Nov 7, 2018

遇到同样的问题,请问解决了么

@PeiXueYang
Copy link

same problem.

@dengpan0513
Copy link

同样的情况
image

@myCedar
Copy link

myCedar commented Jan 9, 2019

遇到同样的问题,请问解决了么

@dingpei
Copy link

dingpei commented Jan 21, 2019

同问

@xuyao91
Copy link

xuyao91 commented Jan 31, 2019

same problem

@9012wushuang
Copy link

?

@luichooy
Copy link

解决方案:

  1. 在router/index.js 中
export const createRouter = () => new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes 
});

2.在需要使用addRoutes的地方,导入该函数

import router, { createRouter } from '@/router';

...

// router是从上面导入的,也就是 main.js 文件中 new Vue(options)中的router
router.matcher = createRouter().matcher;
router.addRoutes(routes);

@luozheao
Copy link

luozheao commented Apr 1, 2019

@luichooy
我精简了一下

  1. 在router/index.js 中
    router.$addRoutes = (params) => {
    router.matcher = new Router({mode: 'history'}).matcher
    router.addRoutes(params)
    }
  2. 在使用的地方
    this.$router.$addRoutes(routes)

@MontageD
Copy link

MontageD commented Jun 4, 2019

楼上正解,亲测有效

@hankun8645
Copy link

router.matcher = createRouter().matcher; 这个方法并不生效,有知道怎么回事吗

@hankun8645
Copy link

各位大佬们 match 并不生效 如何解决啊

@hankun8645
Copy link

我的微信1005922744

@DavidMGT
Copy link

亲测有效,谢谢楼主

@mirrors-cl
Copy link

router.matcher = createRouter().matcher; 这个方法并不生效,有知道怎么回事吗

最后你解决了吗,可以发出来看看吗

@hankun8645
Copy link

我在退出的时候 通过window.loacation.href的方式解决了,那个方法我这边不行

@is0xb
Copy link

is0xb commented Jul 24, 2020

router.matcher = createRouter().matcher; 这个方法并不生效,有知道怎么回事吗

你可能需要在你的环境中使用this.$router来代替router

@vscchencc
Copy link

same problem

使用了上述方式,都不生效

@niujiangyao
Copy link

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const createRouter = () => new Router({
mode: 'history',
routes: []
})

const router = createRouter()

export function resetRouter () {
const newRouter = createRouter()
router.matcher = newRouter.matcher // the relevant part
}

export default router

#1234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests