vue3 vue-router 匹配多级路由

本文深入探讨Vue Router中的嵌套路由行为,包括如何匹配当前路由,以及别名和命名路由的活性。当路径参数与渲染路由相同时,嵌套路由才匹配。别名在同一路径下保持活性,而绝对路径的嵌套别名也有其特定的活性规则。此外,命名路由在解析URL时会考虑空路径子路由,影响链接的活性状态。重复参数时,所有参数必须按顺序完全匹配才能使链接同时处于活动和精确活动状态。
摘要由CSDN通过智能技术生成

Nested Routes

It’s worth noting that nested routes will match only if the params relevant to the rendered route are the same.

E.g., given these routes:

const routes = [
  {
    path: '/parent/:id',
    children: [
      // empty child
      { path: '' },
      // child with id
      { path: 'child/:id' },
      { path: 'child-second/:id' }
    ]
  }
]

If the current route is /parent/1/child/2, these links will be active:

urlactiveexact active
/parent/1/child/2
/parent/1/child/3
/parent/1/child-second/2
/parent/1
/parent/2
/parent/2/child/2
/parent/2/child-second/2

Unrelated but similiar routes

Routes that are unrelated from a record point of view but share a common path are no longer active.

E.g., given these routes:

const routes = [
  { path: '/movies' },
  { path: '/movies/new' },
  { path: '/movies/search' }
]

If the current route is /movies/new, these links will be active:

urlactiveexact active
/movies
/movies/new
/movies/search

Note: This behavior is different from actual behavior.

It’s worth noting, it is possible to nest them to still benefit from links being active:

// Vue 3
import { h } from 'vue'
import { RouterView } from 'vue-router'

const routes = [
  {
    path: '/movies',
    // we need this to render the children (see note below)
    component: { render: () => h(RouterView) },
    // for vue 2 use render: h => h('RouterView')
    children: [
      { path: 'new' },
      // different child
      { path: 'search' }
    ]
  }
]

If the current route is /movies/new, these links will be active:

urlactiveexact active
/movies
/movies/new
/movies/search

Note: To make this easier to use, we could maybe allow component to be absent and internally behave as if there where a component option that renders a RouterView component

Alias

Given that an alias is only a different path while keeping everything else on a record, it makes sense for aliases to be active when the path they are aliasing is matched and vice versa.

E.g., given these routes:

const routes = [{ path: '/movies', alias: ['/films'] }]

If the current route is /movies or /films, both links will be active:

urlactiveexact active
/movies
/films

Nested aliases

The behavior is similar when dealing with nested children of an aliased route.

E.g., given these routes:

const routes = [
  {
    path: '/parent/:id',
    alias: '/p/:id',
    children: [
      // empty child
      { path: '' },
      // child with id
      { path: 'child/:id', alias: 'c/:id' }
    ]
  }
]

If the current route is /parent/1/child/2, /p/1/child/2, /p/1/c/2, or, /parent/1/c/2 these links will be active:

urlactiveexact active
/parent/1/child/2
/parent/1/c/2
/p/1/child/2
/p/1/c/2
/p/1/child/3
/parent/1/child/3
/parent/1
/p/1
/parent/2
/p/2

Absolute nested aliases

Nested children can have an absolute path by making it start with /, in this scenario the same rules apply. Given these routes:

E.g., given these routes:

const routes = [
  {
    path: '/parent/:id',
    alias: '/p/:id',
    name: 'parent',
    children: [
      // empty child
      { path: '', alias: ['alias', '/p_:id'], name: 'child' },
      // child with absolute path. we need to add an `id` because the parent needs it
      { path: '/p_:id/absolute-a', alias: 'as-absolute-a' },
      // same as above but the alias is absolute
      { path: 'as-absolute-b', alias: '/p_:id/absolute-b' }
    ]
  }
]

If the current route is /p_1/absolute-a, /p/1/as-absolute-a, or, /parent/1/as-absolute-a, these links will be active:

urlactiveexact active
/p/1/as-absolute-a
/p_1/absolute-a
/parent/1/absolute-a
/parent/2/absolute-a
/parent/1/absolute-b
/p/1/absolute-b
/p_1/absolute-b
/parent/1
/p/1
/parent/1/alias
/p/1/alias
/p_1
/parent/2
/p/2

Notice how the empty path record is active but not exact active differently from the other child /p/1/absolute-b. All its aliases are active as well because they are aliases of an empty path. If it was the other way around: the path wasn’t empty but one of the aliases was an empty path, then none of them would be active because the original path takes precedence over aliases.

Named nested routes

If the url is resolved through the name of the parent, in this case parent, it will not include the empty path child route. This is important because they both resolve to the same url but when used in router-link's to prop, they would yield different results when it comes to being active and/or exact active. This is consistent with what they render being different and the rest of the active behavior.

E.g., given the routes from the previous example, if the current location is /parent/1 and, both the parent and child views are rendering, meaning we are effectively at { name: 'child' } and not at { name: 'parent' }, here is a similar table to the ones before but also including to:

to's valueresolved urlactiveexact active
{ name: 'parent', params: { id: '1' } }/parent/1 (parent)
'/parent/1'/parent/1 (child)
{ name: 'child', params: { id: '1' } }/parent/1 (child)
'/p_1'/p_1 (child)
'/parent/1/alias'/parent/1/alias (child)

But if the current location is { name: 'parent' }, it will still yield the same url, /parent/1, but a different table:

to's valueresolved urlactiveexact active
{ name: 'parent', params: { id: '1' } }/parent/1 (parent)
'/parent/1'/parent/1 (child)
{ name: 'child', params: { id: '1' } }/parent/1 (child)
'/p_1'/p_1 (child)
'/parent/1/alias'/parent/1/alias (child)

Repeated params

With repeating params like

  • /articles/:id+
  • /articles/:id*

All params must match, with the same exact order for a link to be both, active and exact active.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值