[英]ant-design breadcrumbs with ID
Let's say I have this routing in my react App ( Router
is a BrowserRouter
here)假设我的反应应用程序中有这个路由(
Router
is a BrowserRouter
here)
<Router>
<PageHeader />
<Switch>
<PrivateRoute exact path="/" component={HomePage} />
<PrivateRoute exact path="/profile" component={Profile} />
<PrivateRoute
exact
path="/project/:projectName"
component={Project}
/>
</Switch>
</Router>
I want to display breadcrumbs in my pageheader.我想在我的页眉中显示面包屑。 Ant Design docs explain how to do this with fixed paths here - by fixed I mean ones like
/
or /profile
. Ant 设计文档在这里解释了如何使用固定路径执行此操作 - 固定路径是指像
/
或/profile
这样的路径。
What I want to do is for example display a breadcrumb like Home / FooBar
if user goes to /projects/FooBar
.例如,如果用户转到
/projects/FooBar
,我想要做的是显示像Home / FooBar
这样的面包屑。
Is there a way to do it?有没有办法做到这一点? When I use
withRouter
on my PageHeader
, the location has a pathname of /project/FooBar
, but for example match
has path: "/"
, url: "/"
and no params (which I expected to contain ID).当我在我的
PageHeader
上使用withRouter
时,该位置的路径名是/project/FooBar
,但例如match
具有path: "/"
、 url: "/"
并且没有参数(我希望包含 ID)。
So the question is: is there a way to display breadcrumbs like Home /:projectName
?所以问题是:有没有办法显示像
Home /:projectName
这样的面包屑? I can build them with a 3rd party App, but I would want them to go into either ant design's PageHeader
(as props) or to ant design's BreadCrumb
component (as children)我可以使用第 3 方应用程序构建它们,但我希望它们将 go 放入 ant 设计的
PageHeader
(作为道具)或 ant 设计BreadCrumb
Have you tried using react-router hook useParams ?您是否尝试过使用 react-router 挂钩useParams ?
If your Route is defined as <Route path="/project/:projectName" />
you should be able to do something like this:如果您的 Route 定义为
<Route path="/project/:projectName" />
您应该能够执行以下操作:
const { projectName } = useParams();
<Breadcrumb>
<Breadcrumb.Item>{`Home / ${projectName}`}</Breadcrumb.Item>
</Breadcrumb>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.