1

有人在 remix.config 中有一些关于 defineRoutes 函数的额外信息吗?

我有一条这条路线:

{     
"id": "routes/__main/city/$city", "path": "/city/:city",
"file":"routes/__main/city/$city.tsx"
}

在defineRoutes中我做了这样的事情:

routes: async (defineRoutes) => {
    return defineRoutes((route) => {
      route("/citta/:city", "routes/__main/city/$city.tsx"); 
    });
  },

我希望 /citta/test 和 /city/test 都在位于此处的同一个文件routes/__main/city/$city.tsx中。

但是当我运行代码时,只有 /citta/test 路由处于活动状态,而另一个 /city/test 会抛出错误。

正如我从这里的文档https://remix.run/docs/en/v1/api/conventions#routes中读到的,我想要实现的应该是可能的。

我误解了defineRoutes的使用吗?

4

1 回答 1

1

这可以在不使用的情况下解决defineRoutes。还原您的remix.config更改并让 Remix 为您处理路由,方法是将您的路由放置在app/routes.

移入routes/__main/city/$city.tsx您的app目录并添加一个额外的文件夹结构app/routes/__main/citta/$city.tsx。因此,您有两个文件夹/city并且/citta彼此相邻。他们将共享您引入的所有嵌套路由__main

从您的文件中导出以下内容app/routes/__main/citta/$city.tsx

import CityComponent from '~/routes/__main/city/$city';

// re-export loader, action, and all other functionality
export * from '~/routes/__main/city/$city'
// re-use the default export from your other route
export default CityComponent;

这使您可以city/$city.tsxcitta/$city.tsx.

注意:确保将两个文件都命名为citta/city/ $city.tsx避免命名差异。否则,您重新导出的加载程序和操作将无法正常工作,因为参数名称不同。

于 2022-01-17T18:44:57.980 回答