【发布时间】:2021-12-28 05:49:08
【问题描述】:
所以,假设我有个人资料页面,它根据路径呈现不同的组件。 例如:
/profile/posts 在 Profile 中渲染 Posts 组件。
/profile/comments 在 Profile 中呈现 Comments 组件。
我通常先渲染"/profile"路径的Profile页面,然后在里面,根据上面提到的Routes渲染相关的组件。我想做的是,省略 Profile 内 Route 组件中定义的路径的 "/profile" 部分,只保留剩余部分。在不破坏路由的情况下如何做到这一点?
见下面代码中的cmets:
// App.js
// ...
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/profile"> // Profile page is rendered here
<Profile />
</Route>
</Switch>
// ...
// Profile.js
// ...
<Switch>
<Route exact path="/profile/posts"> // I want to omit "/profile" part of the path.
<Posts />
</Route>
<Route path="/profile/comments"> // I want to omit "/profile" part of the path.
<Comments />
</Route>
</Switch>
// ...
【问题讨论】:
标签: javascript reactjs routes react-router react-router-dom