@react-router/fs-routes
包支持基于文件约定的路由配置。
首先安装 @react-router/fs-routes
包
npm i @react-router/fs-routes
然后使用它在你的 app/routes.ts
文件中提供路由配置
import { type RouteConfig } from "@react-router/dev/routes";
import { flatRoutes } from "@react-router/fs-routes";
export default flatRoutes() satisfies RouteConfig;
默认情况下,app/routes
目录中的任何模块都将成为你应用程序中的路由。ignoredRouteFiles
选项允许你指定不应包含为路由的文件
import { type RouteConfig } from "@react-router/dev/routes";
import { flatRoutes } from "@react-router/fs-routes";
export default flatRoutes({
ignoredRouteFiles: ["home.tsx"],
}) satisfies RouteConfig;
默认情况下,这将在 app/routes
目录中查找路由,但这可以通过 rootDirectory
选项进行配置,该选项是相对于你的应用程序目录的
import { type RouteConfig } from "@react-router/dev/routes";
import { flatRoutes } from "@react-router/fs-routes";
export default flatRoutes({
rootDirectory: "file-routes",
}) satisfies RouteConfig;
本指南的其余部分将假定你正在使用默认的 app/routes
目录。
文件名映射到路由的 URL 路径名,除了 _index.tsx
,它是根路由的索引路由。你可以使用 .js
、.jsx
、.ts
或 .tsx
文件扩展名。
app/
├── routes/
│ ├── _index.tsx
│ └── about.tsx
└── root.tsx
URL | 匹配的路由 |
---|---|
/ |
app/routes/_index.tsx |
/about |
app/routes/about.tsx |
注意,这些路由将因为嵌套路由而渲染在 app/root.tsx
的 outlet 中。
在路由文件名中添加 .
将在 URL 中创建一个 /
。
app/
├── routes/
│ ├── _index.tsx
│ ├── about.tsx
│ ├── concerts.trending.tsx
│ ├── concerts.salt-lake-city.tsx
│ └── concerts.san-diego.tsx
└── root.tsx
URL | 匹配的路由 |
---|---|
/ |
app/routes/_index.tsx |
/about |
app/routes/about.tsx |
/concerts/trending |
app/routes/concerts.trending.tsx |
/concerts/salt-lake-city |
app/routes/concerts.salt-lake-city.tsx |
/concerts/san-diego |
app/routes/concerts.san-diego.tsx |
点分隔符也会创建嵌套,更多信息请参阅嵌套部分。
通常你的 URL 不是静态的,而是数据驱动的。动态段允许你匹配 URL 的段并在代码中使用该值。你使用 $
前缀来创建它们。
app/
├── routes/
│ ├── _index.tsx
│ ├── about.tsx
│ ├── concerts.$city.tsx
│ └── concerts.trending.tsx
└── root.tsx
URL | 匹配的路由 |
---|---|
/ |
app/routes/_index.tsx |
/about |
app/routes/about.tsx |
/concerts/trending |
app/routes/concerts.trending.tsx |
/concerts/salt-lake-city |
app/routes/concerts.$city.tsx |
/concerts/san-diego |
app/routes/concerts.$city.tsx |
该值将从 URL 中解析并传递给各种 API。我们将这些值称为“URL 参数”。访问 URL 参数的最有用之处是在 加载器(loaders)和 操作(actions)中。
export async function serverLoader({ params }) {
return fakeDb.getAllConcertsForCity(params.city);
}
你会注意到 params
对象上的属性名直接映射到你的文件名:$city.tsx
变为 params.city
。
路由可以有多个动态段,例如 concerts.$city.$date
,它们都可以通过名称在 params 对象上访问
export async function serverLoader({ params }) {
return fake.db.getConcerts({
date: params.date,
city: params.city,
});
}
更多信息请参阅路由指南。
嵌套路由是关于将 URL 的段与组件层级和数据关联的普遍概念。你可以在路由指南中阅读更多相关信息。
你使用点分隔符创建嵌套路由。如果文件名中 .
前的部分与另一个路由文件名匹配,它将自动成为匹配父路由的子路由。考虑这些路由:
app/
├── routes/
│ ├── _index.tsx
│ ├── about.tsx
│ ├── concerts._index.tsx
│ ├── concerts.$city.tsx
│ ├── concerts.trending.tsx
│ └── concerts.tsx
└── root.tsx
所有以 app/routes/concerts.
开头的路由将成为 app/routes/concerts.tsx
的子路由,并渲染在父路由的 outlet 中。
URL | 匹配的路由 | 布局 |
---|---|---|
/ |
app/routes/_index.tsx |
app/root.tsx |
/about |
app/routes/about.tsx |
app/root.tsx |
/concerts |
app/routes/concerts._index.tsx |
app/routes/concerts.tsx |
/concerts/trending |
app/routes/concerts.trending.tsx |
app/routes/concerts.tsx |
/concerts/salt-lake-city |
app/routes/concerts.$city.tsx |
app/routes/concerts.tsx |
注意,当你添加嵌套路由时,通常会添加一个索引路由,这样当用户直接访问父 URL 时,父级的 outlet 中会渲染一些内容。
例如,如果 URL 是 /concerts/salt-lake-city
,那么 UI 层级将如下所示:
<Root>
<Concerts>
<City />
</Concerts>
</Root>
有时你希望 URL 是嵌套的,但又不想要自动布局嵌套。你可以通过在父段后添加尾随下划线来选择退出嵌套
app/
├── routes/
│ ├── _index.tsx
│ ├── about.tsx
│ ├── concerts.$city.tsx
│ ├── concerts.trending.tsx
│ ├── concerts.tsx
│ └── concerts_.mine.tsx
└── root.tsx
URL | 匹配的路由 | 布局 |
---|---|---|
/ |
app/routes/_index.tsx |
app/root.tsx |
/about |
app/routes/about.tsx |
app/root.tsx |
/concerts/mine |
app/routes/concerts_.mine.tsx |
app/root.tsx |
/concerts/trending |
app/routes/concerts.trending.tsx |
app/routes/concerts.tsx |
/concerts/salt-lake-city |
app/routes/concerts.$city.tsx |
app/routes/concerts.tsx |
注意,/concerts/mine
不再嵌套在 app/routes/concerts.tsx
中,而是嵌套在 app/root.tsx
中。trailing_
下划线创建了一个路径段,但它不创建布局嵌套。
可以将 trailing_
下划线想象成你父级签名末尾的长划线,把你排除在继承之外,从而将后面的段从布局嵌套中移除。
我们称之为无路径路由
有时你想与一组路由共享一个布局,而无需向 URL 添加任何路径段。一个常见的例子是一组身份验证路由,它们具有与公共页面或登录后的应用程序体验不同的页眉/页脚。你可以使用 _leading
下划线来实现这一点。
app/
├── routes/
│ ├── _auth.login.tsx
│ ├── _auth.register.tsx
│ ├── _auth.tsx
│ ├── _index.tsx
│ ├── concerts.$city.tsx
│ └── concerts.tsx
└── root.tsx
URL | 匹配的路由 | 布局 |
---|---|---|
/ |
app/routes/_index.tsx |
app/root.tsx |
/login |
app/routes/_auth.login.tsx |
app/routes/_auth.tsx |
/register |
app/routes/_auth.register.tsx |
app/routes/_auth.tsx |
/concerts |
app/routes/concerts.tsx |
app/routes/concerts.tsx |
/concerts/salt-lake-city |
app/routes/concerts.$city.tsx |
app/routes/concerts.tsx |
可以将 _leading
下划线想象成你拉过文件名的毯子,将文件名从 URL 中隐藏起来。
用括号括起路由段将使该段成为可选的。
app/
├── routes/
│ ├── ($lang)._index.tsx
│ ├── ($lang).$productId.tsx
│ └── ($lang).categories.tsx
└── root.tsx
URL | 匹配的路由 |
---|---|
/ |
app/routes/($lang)._index.tsx |
/categories |
app/routes/($lang).categories.tsx |
/en/categories |
app/routes/($lang).categories.tsx |
/fr/categories |
app/routes/($lang).categories.tsx |
/american-flag-speedo |
app/routes/($lang)._index.tsx |
/en/american-flag-speedo |
app/routes/($lang).$productId.tsx |
/fr/american-flag-speedo |
app/routes/($lang).$productId.tsx |
你可能想知道为什么 /american-flag-speedo
匹配了 ($lang)._index.tsx
路由而不是 ($lang).$productId.tsx
。这是因为当你在可选的动态参数段后面跟着另一个动态参数时,无法可靠地确定像 /american-flag-speedo
这样的单段 URL 应该匹配 /:lang
还是 /:productId
。可选段会优先匹配,因此它将匹配 /:lang
。如果你有这种设置,建议在 ($lang)._index.tsx
的 loader 中检查 params.lang
,如果 params.lang
不是一个有效的语言代码,则为当前/默认语言重定向到 /:lang/american-flag-speedo
。
虽然动态段匹配单个路径段(URL 中两个 /
之间的内容),但 splat 路由将匹配 URL 的其余部分,包括斜杠。
app/
├── routes/
│ ├── _index.tsx
│ ├── $.tsx
│ ├── about.tsx
│ └── files.$.tsx
└── root.tsx
URL | 匹配的路由 |
---|---|
/ |
app/routes/_index.tsx |
/about |
app/routes/about.tsx |
/beef/and/cheese |
app/routes/$.tsx |
/files |
app/routes/files.$.tsx |
/files/talks/react-conf_old.pdf |
app/routes/files.$.tsx |
/files/talks/react-conf_final.pdf |
app/routes/files.$.tsx |
/files/talks/react-conf-FINAL-MAY_2024.pdf |
app/routes/files.$.tsx |
与动态路由参数类似,你可以使用 "*"
键在 splat 路由的 params
上访问匹配路径的值。
export async function serverLoader({ params }) {
const filePath = params["*"];
return fake.getFileInfo(filePath);
}
如果你想让用于这些路由约定的特殊字符成为 URL 的一部分,你可以使用 []
字符来转义这些约定。这对于包含 URL 扩展名的资源路由特别有用。
文件名 | URL |
---|---|
app/routes/sitemap[.]xml.tsx |
/sitemap.xml |
app/routes/[sitemap.xml].tsx |
/sitemap.xml |
app/routes/weird-url.[_index].tsx |
/weird-url/_index |
app/routes/dolla-bills-[$].tsx |
/dolla-bills-$ |
app/routes/[[so-weird]].tsx |
/[so-weird] |
app/routes/reports.$id[.pdf].ts |
/reports/123.pdf |
路由也可以是包含定义路由模块的 route.tsx
文件的文件夹。文件夹中的其余文件不会成为路由。这使你可以将代码组织得更靠近使用它们的路由,而不是在其他文件夹中重复功能名称。
文件夹内的文件对路由路径没有意义,路由路径完全由文件夹名称定义。
考虑这些路由:
app/
├── routes/
│ ├── _landing._index.tsx
│ ├── _landing.about.tsx
│ ├── _landing.tsx
│ ├── app._index.tsx
│ ├── app.projects.tsx
│ ├── app.tsx
│ └── app_.projects.$id.roadmap.tsx
└── root.tsx
其中部分或全部可以成为包含其自己的 route
模块的文件夹。
app/
├── routes/
│ ├── _landing._index/
│ │ ├── route.tsx
│ │ └── scroll-experience.tsx
│ ├── _landing.about/
│ │ ├── employee-profile-card.tsx
│ │ ├── get-employee-data.server.ts
│ │ ├── route.tsx
│ │ └── team-photo.jpg
│ ├── _landing/
│ │ ├── footer.tsx
│ │ ├── header.tsx
│ │ └── route.tsx
│ ├── app._index/
│ │ ├── route.tsx
│ │ └── stats.tsx
│ ├── app.projects/
│ │ ├── get-projects.server.ts
│ │ ├── project-buttons.tsx
│ │ ├── project-card.tsx
│ │ └── route.tsx
│ ├── app/
│ │ ├── footer.tsx
│ │ ├── primary-nav.tsx
│ │ └── route.tsx
│ ├── app_.projects.$id.roadmap/
│ │ ├── chart.tsx
│ │ ├── route.tsx
│ │ └── update-timeline.server.ts
│ └── contact-us.tsx
└── root.tsx
注意,当你将一个路由模块变成一个文件夹时,该路由模块变为 folder/route.tsx
,文件夹中的所有其他模块都不会成为路由。例如:
# these are the same route:
app/routes/app.tsx
app/routes/app/route.tsx
# as are these
app/routes/app._index.tsx
app/routes/app._index/route.tsx