简体   繁体   English

Vue路由器中的确切路径匹配?

[英]Exact path match in Vue router?

I am working on a Vue application and want to set up a exact path match for the default route.我正在开发一个 Vue 应用程序,并希望为默认路由设置一个精确的路径匹配。 I have experience in React and Angular and both gave option exact and pathMatch:'full' so the default route only comes into action if it matches exactly.我在 React 和 Angular 方面有经验,并且都提供了选项exactpathMatch:'full'所以默认路由只有在完全匹配时才会生效。

In vue, when I refresh my app, the default route gets fired and takes the user back to the first page .在 vue 中,当我刷新我的应用程序时,默认路由被触发并将用户带回第一页。 Is there a way to do something like that in Vue?有没有办法在 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")
    }
  ]

Try putting your routes in this order because Vue-router routes order matters.尝试按此顺序放置您的路由,因为 Vue-router 路由顺序很重要。 The matching starts from the top:匹配从顶部开始:

  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?
    }
  ]

I know this is an old question, but maybe somebody else can benefit from it.我知道这是一个老问题,但也许其他人可以从中受益。

It seems like Vue Router uses exact match by default.默认情况下,Vue Router 似乎使用精确匹配。 For example I have something like this, and it opens the given component without the use of an "exact" keyword or anything similar.例如我有这样的东西,它打开给定的组件而不使用“精确”关键字或任何类似的东西。

  {
    name: 'Mailbox',
    path: '/mailbox',
    component: Mailbox
  },
  {
    name: 'Message',
    path: '/mailbox/:messageId',
    component: Message
  }

Also, in the documentation it is shown how to use the asterisk to match all routes that start with a given path.此外, 在文档中还展示了如何使用星号匹配以给定路径开头的所有路由。 We can then assume this is not the default behaviour of Vue Router.然后我们可以假设这不是 Vue Router 的默认行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM