路由匹配遵循一致且可预测的模式。本指南将解释路由树是如何进行匹配的。
当 TanStack Router 处理您的路由树时,所有路由都会自动排序,优先匹配最具体的路由。这意味着无论您定义路由树的顺序如何,路由始终会按照以下顺序排序:
考虑以下伪路由树:
Root
- blog
- $postId
- /
- new
- /
- *
- about
- about/us
Root
- blog
- $postId
- /
- new
- /
- *
- about
- about/us
排序后,该路由树将变为:
Root
- /
- about/us
- about
- blog
- /
- new
- $postId
- *
Root
- /
- about/us
- about
- blog
- /
- new
- $postId
- *
这个最终顺序代表了路由将根据具体性进行匹配的顺序。
使用该路由树,让我们跟踪几个不同 URL 的匹配过程:
Root
❌ /
❌ about/us
❌ about
⏩ blog
✅ /
- new
- $postId
- *
Root
❌ /
❌ about/us
❌ about
⏩ blog
✅ /
- new
- $postId
- *
Root
❌ /
❌ about/us
❌ about
⏩ blog
❌ /
❌ new
✅ $postId
- *
Root
❌ /
❌ about/us
❌ about
⏩ blog
❌ /
❌ new
✅ $postId
- *
Root
✅ /
- about/us
- about
- blog
- /
- new
- $postId
- *
Root
✅ /
- about/us
- about
- blog
- /
- new
- $postId
- *
Root
❌ /
❌ about/us
❌ about
❌ blog
- /
- new
- $postId
✅ *
Root
❌ /
❌ about/us
❌ about
❌ blog
- /
- new
- $postId
✅ *
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.