Vue路由器中的路径完全匹配吗?

时间:2019-08-05 09:49:32

标签: vue.js vuejs2 vue-router

我正在使用Vue应用程序,并且想为默认路由设置精确的路径匹配。我在React和Angular上都有经验,并且都给了选项exactpathMatch:'full',所以默认路由只有在完全匹配的情况下才会生效。

实际上,当我刷新我的应用程序时,默认路由会被触发并将用户带回到第一页。有没有办法在Vue中做类似的事情?

  routes: [
    {
      path: "/",
      name: "home",
      component: Home
      // how can I add exact match config here?
    },
    {
      path: "/login",
      name: "login",
      component: () =>
        import(/* webpackChunkName: "about" */ "./views/Login.vue")
    },
    {
      path: "/charts",
      name: "charts",
      component: () =>
        import(/* webpackChunkName: "about" */ "./views/ChartDemo.vue")
    }
  ]

1 个答案:

答案 0 :(得分:1)

请尝试按此顺序排列您的路线,因为Vue-router的路线顺序很重要。匹配从顶部开始:

  routes: [
    {
      path: "/login",
      name: "login",
      component: () =>
        import(/* webpackChunkName: "about" */ "./views/Login.vue")
    },
    {
      path: "/charts",
      name: "charts",
      component: () =>
        import(/* webpackChunkName: "about" */ "./views/ChartDemo.vue")
    },
    {
      path: "/",
      name: "home",
      component: Home
      // how can I add exact match config here?
    }
  ]