显示当前页面在系统层级结构中的位置,并能向上返回。
// >=5.3.0 可用,推荐的写法 ✅return <Breadcrumb items={[{ title: 'sample' }]} />;// <5.3.0 可用,>=5.3.0 时不推荐 🙅🏻♀️return (<Breadcrumb><Breadcrumb.Item>sample</Breadcrumb.Item></Breadcrumb>);// 或return <Breadcrumb routes={[{ breadcrumbName: 'sample' }]} />;
import React from 'react';
import { Breadcrumb } from 'antd';
const App: React.FC = () => (
<Breadcrumb
items={[
{
title: 'Home',
},
{
title: <a href="">Application Center</a>,
},
{
title: <a href="">Application List</a>,
},
{
title: 'An Application',
},
]}
/>
);
export default App;
import React from 'react';
import { Alert, Breadcrumb } from 'antd';
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 {
key: url,
title: <Link to={url}>{breadcrumbNameMap[url]}</Link>,
};
});
const breadcrumbItems = [
{
title: <Link to="/">Home</Link>,
key: 'home',
},
].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 items={breadcrumbItems} />
</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;
}
import React from 'react';
import { Breadcrumb } from 'antd';
const menuItems = [
{
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
items={[
{
title: 'Ant Design',
},
{
title: <a href="">Component</a>,
},
{
title: <a href="">General</a>,
menu: { items: menuItems },
},
{
title: 'Button',
},
]}
/>
);
export default App;
import { Breadcrumb } from 'antd';
import React from 'react';
export default () => (
<Breadcrumb
routes={[
{
path: '/home',
breadcrumbName: 'Home',
},
{
path: '/user',
breadcrumbName: 'User',
children: [
{
path: '/user1',
breadcrumbName: 'User1',
},
{
path: '/user2',
breadcrumbName: 'User2',
},
],
},
]}
/>
);
import { HomeOutlined, UserOutlined } from '@ant-design/icons';
import { Breadcrumb } from 'antd';
import React from 'react';
const App: React.FC = () => (
<Breadcrumb
items={[
{
href: '',
title: <HomeOutlined />,
},
{
href: '',
title: (
<>
<UserOutlined />
<span>Application List</span>
</>
),
},
{
title: 'Application',
},
]}
/>
);
export default App;
import React from 'react';
import { Breadcrumb } from 'antd';
const App: React.FC = () => (
<Breadcrumb
separator=">"
items={[
{
title: 'Home',
},
{
title: 'Application Center',
href: '',
},
{
title: 'Application List',
href: '',
},
{
title: 'An Application',
},
]}
/>
);
export default App;
import { Breadcrumb } from 'antd';
import React from 'react';
const App: React.FC = () => (
<Breadcrumb
separator=""
items={[
{
title: 'Location',
},
{
type: 'separator',
separator: ':',
},
{
href: '',
title: 'Application Center',
},
{
type: 'separator',
},
{
href: '',
title: 'Application List',
},
{
type: 'separator',
},
{
title: 'An Application',
},
]}
/>
);
export default App;
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
itemRender | 自定义链接函数,和 react-router 配置使用 | (route, params, routes, paths) => ReactNode | - | |
params | 路由的参数 | object | - | |
items | 路由栈信息 | items[] | - | 5.3.0 |
separator | 分隔符自定义 | ReactNode | / |
type ItemType = RouteItemType | SeparatorType
const item = {type: 'separator', // Must haveseparator: '/',};
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
type | 标记为分隔符 | separator | 5.3.0 | |
separator | 要显示的分隔符 | ReactNode | / | 5.3.0 |
和 react-router 一起使用时,默认生成的 url 路径是带有 #
的,如果和 browserHistory 一起使用的话,你可以使用 itemRender
属性定义面包屑链接。
import { Link } from 'react-router';const items = [{path: 'index',title: 'home',},{path: 'first',title: 'first',children: [{path: '/general',title: 'General',},{path: '/layout',title: 'Layout',},{path: '/navigation',title: 'Navigation',},],},{path: 'second',title: 'second',},];function itemRender(item, params, items, paths) {const last = items.indexOf(item) === items.length - 1;return last ? <span>{item.title}</span> : <Link to={paths.join('/')}>{item.title}</Link>;}return <Breadcrumb itemRender={itemRender} items={items} />;