2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
2016-09-21 11:28:38 +08:00
|
|
|
|
subtitle: 面包屑
|
2022-11-09 12:28:04 +08:00
|
|
|
|
group: 导航
|
2016-09-21 11:28:38 +08:00
|
|
|
|
title: Breadcrumb
|
2020-05-31 11:48:34 +08:00
|
|
|
|
cover: https://gw.alipayobjects.com/zos/alicdn/9Ltop8JwH/Breadcrumb.svg
|
2022-11-09 12:28:04 +08:00
|
|
|
|
demo:
|
|
|
|
|
cols: 2
|
2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
2015-06-15 22:01:39 +08:00
|
|
|
|
|
2015-06-26 14:42:30 +08:00
|
|
|
|
显示当前页面在系统层级结构中的位置,并能向上返回。
|
2015-06-17 20:24:42 +08:00
|
|
|
|
|
2015-06-26 14:42:30 +08:00
|
|
|
|
## 何时使用
|
2015-06-17 20:24:42 +08:00
|
|
|
|
|
2015-06-26 14:42:30 +08:00
|
|
|
|
- 当系统拥有超过两级以上的层级结构时;
|
2016-02-18 11:31:49 +08:00
|
|
|
|
- 当需要告知用户『你在哪里』时;
|
2015-06-26 14:42:30 +08:00
|
|
|
|
- 当需要向上导航的功能时。
|
2015-09-15 17:35:22 +08:00
|
|
|
|
|
2022-10-23 00:33:45 +08:00
|
|
|
|
### 4.24.0 用法升级
|
|
|
|
|
|
2022-11-15 22:55:01 +08:00
|
|
|
|
<Alert message="在 4.24.0 版本后,我们提供了 <Breadcrumb.Item menu={{ items: [...] }}> 的简写方式,有更好的性能和更方便的数据组织方式,开发者不再需要自行拼接 JSX。同时我们废弃了原先的写法,你还是可以在 4.x 继续使用,但会在控制台看到警告,并会在 5.0 后移除。"></Alert>
|
2022-10-23 00:33:45 +08:00
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
// >=4.24.0 可用,推荐的写法 ✅
|
|
|
|
|
const items = [
|
|
|
|
|
{ label: '菜单项一', key: 'item-1' }, // 菜单项务必填写 key
|
|
|
|
|
{ label: '菜单项二', key: 'item-2' },
|
|
|
|
|
];
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb>
|
|
|
|
|
<Breadcrumb.Item menu={{ items }}>Ant Design</Breadcrumb.Item>
|
|
|
|
|
</Breadcrumb>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// <4.24.0 可用,>=4.24.0 时不推荐 🙅🏻♀️
|
|
|
|
|
const menu = (
|
|
|
|
|
<Menu>
|
|
|
|
|
<Menu.Item>菜单项一</Menu.Item>
|
|
|
|
|
<Menu.Item>菜单项二</Menu.Item>
|
|
|
|
|
</Menu>
|
|
|
|
|
);
|
|
|
|
|
return (
|
|
|
|
|
<Breadcrumb>
|
|
|
|
|
<Breadcrumb.Item overlay={menu}>Ant Design</Breadcrumb.Item>
|
|
|
|
|
</Breadcrumb>
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
|
## 代码演示
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
<!-- prettier-ignore -->
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/basic.tsx">基本</code>
|
|
|
|
|
<code src="./demo/withIcon.tsx">带有图标的</code>
|
2022-11-17 11:30:20 +08:00
|
|
|
|
<code src="./demo/react-router.tsx" iframe="200">react-router V6</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
<code src="./demo/separator.tsx">分隔符</code>
|
|
|
|
|
<code src="./demo/overlay.tsx">带下拉菜单的面包屑</code>
|
|
|
|
|
<code src="./demo/separator-component.tsx">分隔符</code>
|
|
|
|
|
|
2015-09-15 17:35:22 +08:00
|
|
|
|
## API
|
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
|
### Breadcrumb
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
|
|
|
|
| itemRender | 自定义链接函数,和 react-router 配置使用 | (route, params, routes, paths) => ReactNode | - | |
|
|
|
|
|
| params | 路由的参数 | object | - | |
|
|
|
|
|
| routes | router 的路由栈信息 | [routes\[\]](#routes) | - | |
|
|
|
|
|
| separator | 分隔符自定义 | ReactNode | `/` | |
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
|
|
|
|
### Breadcrumb.Item
|
|
|
|
|
|
2022-10-23 00:33:45 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| ------------- | ------------------------ | ---------------------------------- | ------ | ------ |
|
|
|
|
|
| className | 自定义类名 | string | - | |
|
|
|
|
|
| dropdownProps | 弹出下拉菜单的自定义配置 | [Dropdown](/components/dropdown) | - | |
|
|
|
|
|
| href | 链接的目的地 | string | - | |
|
|
|
|
|
| menu | 菜单配置项 | [MenuProps](/components/menu/#API) | - | 4.24.0 |
|
|
|
|
|
| onClick | 单击事件 | (e:MouseEvent) => void | - | |
|
2019-05-06 12:04:39 +08:00
|
|
|
|
|
2019-11-20 18:10:44 +08:00
|
|
|
|
### Breadcrumb.Separator
|
2019-07-31 15:00:00 +08:00
|
|
|
|
|
2022-10-23 00:33:45 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| -------- | -------------- | --------- | ------ | ---- |
|
|
|
|
|
| children | 要显示的分隔符 | ReactNode | `/` | |
|
2019-07-31 15:00:00 +08:00
|
|
|
|
|
|
|
|
|
> 注意:在使用 `Breadcrumb.Separator` 时,其父组件的分隔符必须设置为 `separator=""`,否则会出现父组件默认的分隔符。
|
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
|
### routes
|
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
interface Route {
|
|
|
|
|
path: string;
|
|
|
|
|
breadcrumbName: string;
|
|
|
|
|
children: Array<{
|
|
|
|
|
path: string;
|
|
|
|
|
breadcrumbName: string;
|
|
|
|
|
}>;
|
|
|
|
|
}
|
|
|
|
|
```
|
2016-08-29 16:52:35 +08:00
|
|
|
|
|
2016-12-04 15:17:17 +08:00
|
|
|
|
### 和 browserHistory 配合
|
|
|
|
|
|
|
|
|
|
和 react-router 一起使用时,默认生成的 url 路径是带有 `#` 的,如果和 browserHistory 一起使用的话,你可以使用 `itemRender` 属性定义面包屑链接。
|
|
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
|
import { Link } from 'react-router';
|
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
|
const routes = [
|
|
|
|
|
{
|
|
|
|
|
path: 'index',
|
|
|
|
|
breadcrumbName: 'home',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'first',
|
|
|
|
|
breadcrumbName: 'first',
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: '/general',
|
|
|
|
|
breadcrumbName: 'General',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/layout',
|
|
|
|
|
breadcrumbName: 'Layout',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/navigation',
|
|
|
|
|
breadcrumbName: 'Navigation',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'second',
|
|
|
|
|
breadcrumbName: 'second',
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2016-12-04 15:17:17 +08:00
|
|
|
|
function itemRender(route, params, routes, paths) {
|
|
|
|
|
const last = routes.indexOf(route) === routes.length - 1;
|
2019-05-06 12:04:39 +08:00
|
|
|
|
return last ? (
|
|
|
|
|
<span>{route.breadcrumbName}</span>
|
|
|
|
|
) : (
|
|
|
|
|
<Link to={paths.join('/')}>{route.breadcrumbName}</Link>
|
|
|
|
|
);
|
2016-12-04 15:17:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
|
return <Breadcrumb itemRender={itemRender} routes={routes} />;
|
2016-12-04 15:17:17 +08:00
|
|
|
|
```
|