gpt4 book ai didi

Sharing data with Remix nested routes, while making parent route not accessible (Routing V2)(与Remix嵌套路由共享数据,同时使父路由不可访问(路由V2))

转载 作者:bug小助手 更新时间:2023-10-25 19:43:30 33 4
gpt4 key购买 nike



New to Remix, I am trying to achieve a specific routing solution with the V2 routes approach.

我是Remix的新手,我正在尝试使用V2路由方法实现特定的路由解决方案。


This is a contrived example, not the real thing, just to illustrate the point...

这是一个人为的例子,不是真实的东西,只是为了说明这一点。


The URL scheme I want is this:

我想要的URL方案是:


/user
/user/edit/one
/user/edit/two

I start with these routes:

我从以下几条路线开始:


app/routes/user/route.tsx
app/routes/user.edit/route.tsx
app/routes/user.edit.one/route.tsx
app/routes/user.edit.two/route.tsx

Purpose of each route


app/routes/user/route.tsx:

App/routes/user/route.tsx:



  • in loader, fetch user record from database

  • unique UI, display a read-only view of the user


app/routes/user.edit/route.tsx:

App/routes/user.dit/route.tsx:



  • use already fetched user record from parent route via useOutletContext

  • renders a template UI that is independent of the parent route, but has a common layout with an Outlet for child routes

  • this route should not be directly navigable


app/routes/user.edit.one/route.tsx:

App/routes/user.edit.one/route.tsx:



  • use already fetched user record from grandparent route via useOutletContext

  • UI renders in the immediate parent layout Outlet


app/routes/user.edit.two/route.tsx:

App/routes/user.edit.Two/route.tsx:



  • use already fetched user record from grandparent route via useOutletContext

  • UI renders in the immediate parent layout Outlet


This seems reasonable to me.

这对我来说似乎是合理的。


The problem


The problem here is that the edit child routes would be rendered within the view-only Outlet, but the UI is supposed to be completely unique, separate from the view-only UI.

这里的问题是,编辑子路线将在仅供查看的插座中呈现,但用户界面应该是完全唯一的,与仅供查看的用户界面分开。


So, I change it this way with trailing underscore for the "edit" routes:

因此,我将其更改为“编辑”路线的尾部下划线:


app/routes/user/route.tsx
app/routes/user_.edit/route.tsx
app/routes/user_.edit.one/route.tsx
app/routes/user_.edit.two/route.tsx

With this, the URL scheme is still as inteded, but now it no longer loads the user record from the parent view-only route.

这样,URL方案仍然是集成的,但现在它不再从仅供查看的父路由加载用户记录。


This is actually not so bad, I can add the code to load the user record to the edit route. It's a little bit of duplication, but it's fine.

这实际上并不是很糟糕,我可以添加代码来将用户记录加载到编辑路线。这是有点重复,但它很好。


The problem now is that app/routes/user_.edit/route.tsx is not supposed to be navigable by the user, there is nothing to display at this route as it just a common layout template for all the child routes. It should go direct to the first child route instead.

现在的问题是,用户不应该可以导航app/routes/user_.dit/route.tsx,这条路线上没有什么可显示的,因为它只是所有子路线的通用布局模板。它应该直接转到第一条子路由。


How can I achieve this?

我如何才能做到这一点?


Related


There's a similar discussion here using the old routing approach: https://github.com/remix-run/remix/discussions/3464

这里有一个使用旧的路由方法的类似讨论:https://github.com/remix-run/remix/discussions/3464


This question is also similar: How to make a nested route parent not accessible remix

这个问题也是类似的:如何使嵌套路由父路径无法访问混音


更多回答
优秀答案推荐

By default, Remix will render the "layout" route at the URL /user/edit. To prevent that, you should create an index route and add a loader that redirects to the one route.

默认情况下,Remix将在URL/USER/EDIT处呈现“Layout”路线。为了防止出现这种情况,您应该创建一个索引路由,并添加一个重定向到该路由的加载器。


https://remix.run/docs/en/v1/guides/routing#index-routes

Https://remix.run/docs/en/v1/guides/routing#index-routes


// routes/user_.edit._index/route.tsx

export const loader = () => redirect('one')
export default function Component() {
return null
}

更多回答

Perfect, thanks. I think you can even forgo the default Component export here too.

太好了,谢谢。我认为您甚至可以放弃这里的默认组件导出。

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com