Breadcrumb面包屑
显示当前页面在系统层级结构中的位置,并能向上返回。
何时使用#
当系统拥有超过两级以上的层级结构时;
当需要告知用户『你在哪里』时;
当需要向上导航的功能时。
4.24.0 用法升级#
// >=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>
);
代码演示
TypeScript
JavaScript
import { Breadcrumb } from 'antd';
import React from 'react';
const App: React.FC = () => (
<Breadcrumb>
<Breadcrumb.Item>Home</Breadcrumb.Item>
<Breadcrumb.Item>
<a href="">Application Center</a>
</Breadcrumb.Item>
<Breadcrumb.Item>
<a href="">Application List</a>
</Breadcrumb.Item>
<Breadcrumb.Item>An Application</Breadcrumb.Item>
</Breadcrumb>
);
export default App;
TypeScript
JavaScript
import { Alert, Breadcrumb } from 'antd';
import React from 'react';
import { HashRouter, Link, Route, Routes, useLocation } from 'react-router-dom';
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>
);
const breadcrumbNameMap: Record<string, string> = {
'/apps': 'Application List',
'/apps/1': 'Application1',
'/apps/2': 'Application2',
'/apps/1/detail': 'Detail',
'/apps/2/detail': 'Detail',
};
const Home = () => {
const location = useLocation();
const pathSnippets = location.pathname.split('/').filter(i => i);
const extraBreadcrumbItems = pathSnippets.map((_, index) => {
const url = `/${pathSnippets.slice(0, index + 1).join('/')}`;
return (
<Breadcrumb.Item key={url}>
<Link to={url}>{breadcrumbNameMap[url]}</Link>
</Breadcrumb.Item>
);
});
const breadcrumbItems = [
<Breadcrumb.Item key="home">
<Link to="/">Home</Link>
</Breadcrumb.Item>,
].concat(extraBreadcrumbItems);
return (
<div className="demo">
<div className="demo-nav">
<Link to="/">Home</Link>
<Link to="/apps">Application List</Link>
</div>
<Routes>
<Route path="/apps" element={<Apps />} />
<Route path="*" element={<span>Home Page</span>} />
</Routes>
<Alert style={{ margin: '16px 0' }} message="Click the navigation above to switch:" />
<Breadcrumb>{breadcrumbItems}</Breadcrumb>
</div>
);
};
const App: React.FC = () => (
<HashRouter>
<Home />
</HashRouter>
);
export default App;
.demo {
margin: 16px;
}
.demo-nav {
height: 30px;
margin-bottom: 16px;
line-height: 30px;
background: #f8f8f8;
}
.demo-nav a {
padding: 0 8px;
line-height: 30px;
}
.app-list {
margin-top: 16px;
}
TypeScript
JavaScript
import { Breadcrumb } from 'antd';
import React from 'react';
const items = [
{
key: '1',
label: (
<a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">
General
</a>
),
},
{
key: '2',
label: (
<a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/">
Layout
</a>
),
},
{
key: '3',
label: (
<a target="_blank" rel="noopener noreferrer" href="http://www.tmall.com/">
Navigation
</a>
),
},
];
const App: React.FC = () => (
<Breadcrumb>
<Breadcrumb.Item>Ant Design</Breadcrumb.Item>
<Breadcrumb.Item>
<a href="">Component</a>
</Breadcrumb.Item>
<Breadcrumb.Item menu={{ items }}>
<a href="">General</a>
</Breadcrumb.Item>
<Breadcrumb.Item>Button</Breadcrumb.Item>
</Breadcrumb>
);
export default App;
TypeScript
JavaScript
import { HomeOutlined, UserOutlined } from '@ant-design/icons';
import { Breadcrumb } from 'antd';
import React from 'react';
const App: React.FC = () => (
<Breadcrumb>
<Breadcrumb.Item href="">
<HomeOutlined />
</Breadcrumb.Item>
<Breadcrumb.Item href="">
<UserOutlined />
<span>Application List</span>
</Breadcrumb.Item>
<Breadcrumb.Item>Application</Breadcrumb.Item>
</Breadcrumb>
);
export default App;
TypeScript
JavaScript
import { Breadcrumb } from 'antd';
import React from 'react';
const App: React.FC = () => (
<Breadcrumb separator=">">
<Breadcrumb.Item>Home</Breadcrumb.Item>
<Breadcrumb.Item href="">Application Center</Breadcrumb.Item>
<Breadcrumb.Item href="">Application List</Breadcrumb.Item>
<Breadcrumb.Item>An Application</Breadcrumb.Item>
</Breadcrumb>
);
export default App;
TypeScript
JavaScript
import { Breadcrumb } from 'antd';
import React from 'react';
const App: React.FC = () => (
<Breadcrumb separator="">
<Breadcrumb.Item>Location</Breadcrumb.Item>
<Breadcrumb.Separator>:</Breadcrumb.Separator>
<Breadcrumb.Item href="">Application Center</Breadcrumb.Item>
<Breadcrumb.Separator />
<Breadcrumb.Item href="">Application List</Breadcrumb.Item>
<Breadcrumb.Separator />
<Breadcrumb.Item>An Application</Breadcrumb.Item>
</Breadcrumb>
);
export default App;
API#
Breadcrumb#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
itemRender | 自定义链接函数,和 react-router 配置使用 | (route, params, routes, paths) => ReactNode | - | |
params | 路由的参数 | object | - | |
routes | router 的路由栈信息 | routes[] | - | |
separator | 分隔符自定义 | ReactNode | / |
Breadcrumb.Item#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
className | 自定义类名 | string | - | |
dropdownProps | 弹出下拉菜单的自定义配置 | Dropdown | - | |
href | 链接的目的地 | string | - | |
menu | 菜单配置项 | MenuProps | - | 4.24.0 |
onClick | 单击事件 | (e:MouseEvent) => void | - |
Breadcrumb.Separator#
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
children | 要显示的分隔符 | ReactNode | / |
注意:在使用
Breadcrumb.Separator
时,其父组件的分隔符必须设置为separator=""
,否则会出现父组件默认的分隔符。
routes#
interface Route {
path: string;
breadcrumbName: string;
children: Array<{
path: string;
breadcrumbName: string;
}>;
}
和 browserHistory 配合#
和 react-router 一起使用时,默认生成的 url 路径是带有 #
的,如果和 browserHistory 一起使用的话,你可以使用 itemRender
属性定义面包屑链接。
import { Link } from 'react-router';
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',
},
];
function itemRender(route, params, routes, paths) {
const last = routes.indexOf(route) === routes.length - 1;
return last ? (
<span>{route.breadcrumbName}</span>
) : (
<Link to={paths.join('/')}>{route.breadcrumbName}</Link>
);
}
return <Breadcrumb itemRender={itemRender} routes={routes} />;