2017-04-06 14:10:01 +08:00
|
|
|
|
---
|
|
|
|
|
order: 3
|
|
|
|
|
iframe: 200
|
|
|
|
|
reactRouter: react-router-dom
|
|
|
|
|
title:
|
2021-11-11 21:34:07 +08:00
|
|
|
|
zh-CN: react-router V6
|
|
|
|
|
en-US: react-router V6
|
2017-04-06 14:10:01 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
2021-11-11 21:34:07 +08:00
|
|
|
|
与 `react-router@6+` 结合使用,生成和路由绑定的面包屑。
|
2017-04-06 14:10:01 +08:00
|
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
2021-11-11 21:34:07 +08:00
|
|
|
|
Used together with `react-router@6+`.
|
2017-04-06 14:10:01 +08:00
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
|
import { Alert, Breadcrumb } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
|
import React from 'react';
|
2022-05-23 14:37:16 +08:00
|
|
|
|
import { HashRouter, Link, Route, Routes, useLocation } from 'react-router-dom';
|
2017-04-06 14:10:01 +08:00
|
|
|
|
|
|
|
|
|
const Apps = () => (
|
|
|
|
|
<ul className="app-list">
|
|
|
|
|
<li>
|
|
|
|
|
<Link to="/apps/1">Application1</Link>:<Link to="/apps/1/detail">Detail</Link>
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
<Link to="/apps/2">Application2</Link>:<Link to="/apps/2/detail">Detail</Link>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
);
|
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const breadcrumbNameMap: Record<string, string> = {
|
2017-06-16 14:57:14 +08:00
|
|
|
|
'/apps': 'Application List',
|
|
|
|
|
'/apps/1': 'Application1',
|
|
|
|
|
'/apps/2': 'Application2',
|
|
|
|
|
'/apps/1/detail': 'Detail',
|
|
|
|
|
'/apps/2/detail': 'Detail',
|
|
|
|
|
};
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
|
|
const Home = () => {
|
2021-11-11 21:34:07 +08:00
|
|
|
|
const location = useLocation();
|
2017-06-16 14:57:14 +08:00
|
|
|
|
const pathSnippets = location.pathname.split('/').filter(i => i);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
2017-06-16 14:57:14 +08:00
|
|
|
|
const extraBreadcrumbItems = pathSnippets.map((_, index) => {
|
|
|
|
|
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb.Item key={url}>
|
2019-05-07 14:57:32 +08:00
|
|
|
|
<Link to={url}>{breadcrumbNameMap[url]}</Link>
|
2017-06-16 14:57:14 +08:00
|
|
|
|
</Breadcrumb.Item>
|
|
|
|
|
);
|
|
|
|
|
});
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
|
const breadcrumbItems = [
|
2017-06-16 14:57:14 +08:00
|
|
|
|
<Breadcrumb.Item key="home">
|
2017-04-06 14:10:01 +08:00
|
|
|
|
<Link to="/">Home</Link>
|
2019-05-07 14:57:32 +08:00
|
|
|
|
</Breadcrumb.Item>,
|
|
|
|
|
].concat(extraBreadcrumbItems);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
2017-06-16 14:57:14 +08:00
|
|
|
|
return (
|
|
|
|
|
<div className="demo">
|
|
|
|
|
<div className="demo-nav">
|
|
|
|
|
<Link to="/">Home</Link>
|
|
|
|
|
<Link to="/apps">Application List</Link>
|
|
|
|
|
</div>
|
2021-11-11 21:34:07 +08:00
|
|
|
|
<Routes>
|
2022-01-04 21:45:05 +08:00
|
|
|
|
<Route path="/apps" element={<Apps />} />
|
|
|
|
|
<Route path="*" element={<span>Home Page</span>} />
|
2021-11-11 21:34:07 +08:00
|
|
|
|
</Routes>
|
2017-06-16 14:57:14 +08:00
|
|
|
|
<Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
|
2019-05-07 14:57:32 +08:00
|
|
|
|
<Breadcrumb>{breadcrumbItems}</Breadcrumb>
|
2017-04-06 14:10:01 +08:00
|
|
|
|
</div>
|
2017-06-16 14:57:14 +08:00
|
|
|
|
);
|
2021-11-11 21:34:07 +08:00
|
|
|
|
};
|
2017-04-06 14:10:01 +08:00
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
|
const App: React.FC = () => (
|
2021-11-11 21:34:07 +08:00
|
|
|
|
<HashRouter>
|
2017-06-16 14:57:14 +08:00
|
|
|
|
<Home />
|
2022-04-03 23:27:45 +08:00
|
|
|
|
</HashRouter>
|
2018-11-28 15:00:03 +08:00
|
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
|
```
|
2017-04-06 14:10:01 +08:00
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
|
```css
|
2017-04-06 14:10:01 +08:00
|
|
|
|
.demo {
|
|
|
|
|
margin: 16px;
|
|
|
|
|
}
|
|
|
|
|
.demo-nav {
|
|
|
|
|
height: 30px;
|
|
|
|
|
margin-bottom: 16px;
|
2021-01-30 19:39:02 +08:00
|
|
|
|
line-height: 30px;
|
2017-04-06 14:10:01 +08:00
|
|
|
|
background: #f8f8f8;
|
|
|
|
|
}
|
|
|
|
|
.demo-nav a {
|
|
|
|
|
padding: 0 8px;
|
2021-01-30 19:39:02 +08:00
|
|
|
|
line-height: 30px;
|
2017-04-06 14:10:01 +08:00
|
|
|
|
}
|
|
|
|
|
.app-list {
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
|
```
|
2020-01-02 17:54:11 +08:00
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
[data-theme="dark"] .demo-nav {
|
|
|
|
|
background: #141414;
|
|
|
|
|
}
|
|
|
|
|
</style>
|