vue-router动态路由匹配了正常的路由怎么办?

问题描述

当定义一个动态路由如/user/:id时,在定义一个正常路由/user/edit,但是当访问后者的时候,跳转到了前面的路由,不知道该怎么匹配

相关代码

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      component: () => import('./views/index/Index.vue'),
      children: [
        {
          path: '/',
          redirect: {
            name: 'index'
          }
        },
        {
          path: '/index',
          name: 'index',
          component: () => import('./views/pages/Home.vue')
        },
        {
          path: 'user/:id',
          name: 'user_post',
          component: ()=>import('./views/pages/Article.vue')
        }
      ]
    },
    {
      path: '/user',
      component: () => import('./views/users/User.vue'),
      children: [
        {
          path: '',
          redirect: {
            name: 'profile'
          }
        },
        {
          path: 'profile',
          name: 'profile',
          component: () => import('./components/user/Profile.vue')
        },
        {
          path: 'editor',
          name: 'editor',
          component: () => import('./components/user/Editor.vue')
        }
      ]
    }
  ]
})

我期望的是首页中通过用户id访问用户的首页,然后/user/profile/user/editor等是登录之后用户首页信息、编辑文章等的路由,但是现在访问/user/editor直接跳转到/user/:id即user_post的路由了,不知道有什么好的解决办法吗?

阅读 2.3k
1 个回答

之前也遇到过,但是没想到办法,只能将/user/:id转换成query访问

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题