2016-03-31 14:17:09 +08:00
|
|
|
---
|
|
|
|
category: Components
|
2016-09-21 11:28:38 +08:00
|
|
|
subtitle: 面包屑
|
2018-11-04 16:20:35 +08:00
|
|
|
type: 导航
|
2016-09-21 11:28:38 +08:00
|
|
|
title: Breadcrumb
|
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
|
|
|
|
|
|
|
## API
|
|
|
|
|
2019-05-06 12:04:39 +08:00
|
|
|
### Breadcrumb
|
|
|
|
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 |
|
|
|
|
| --- | --- | --- | --- |
|
|
|
|
| itemRender | 自定义链接函数,和 react-router 配置使用 | (route, params, routes, paths) => ReactNode | - |
|
|
|
|
| params | 路由的参数 | object | - |
|
|
|
|
| routes | router 的路由栈信息 | [routes\[\]](#routes) | - |
|
|
|
|
| separator | 分隔符自定义 | string\|ReactNode | '/' |
|
|
|
|
|
|
|
|
### Breadcrumb.Item
|
|
|
|
|
|
|
|
| 参数 | 参数 | 类型 | 默认值 |
|
|
|
|
| --------- | -------------- | -------------------------------------- | ------ |
|
|
|
|
| href | 链接的目的地 | string | - |
|
|
|
|
| separator | 自定义的分隔符 | string\|ReactNode | '/' |
|
|
|
|
| overlay | 下来菜单的内容 | [Menu](/components/menu) \| () => Menu | - |
|
|
|
|
| onClick | 单击事件 | (e:MouseEventHandler)=>void | - |
|
|
|
|
|
|
|
|
### 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
|
|
|
```
|