将 Material-UI 应用程序的抽屉放在页眉下面可以通过以下步骤实现:
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Drawer from '@material-ui/core/Drawer';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
},
drawer: {
width: 240,
flexShrink: 0,
},
drawerPaper: {
width: 240,
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
},
}));
const App = () => {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="fixed" className={classes.appBar}>
<Toolbar>
<IconButton edge="start" color="inherit" aria-label="menu">
<MenuIcon />
</IconButton>
{/* 在这里添加页眉内容 */}
</Toolbar>
</AppBar>
<Drawer
className={classes.drawer}
variant="permanent"
classes={{
paper: classes.drawerPaper,
}}
>
{/* 在这里添加抽屉内容 */}
</Drawer>
<main className={classes.content}>
{/* 在这里添加主要内容 */}
</main>
</div>
);
};
export default App;
AppBar
组件中添加页眉内容,可以使用 Typography
组件来显示文本:import Typography from '@material-ui/core/Typography';
// ...
<AppBar position="fixed" className={classes.appBar}>
<Toolbar>
<IconButton edge="start" color="inherit" aria-label="menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
页眉内容
</Typography>
</Toolbar>
</AppBar>
Drawer
组件中添加抽屉内容,可以使用 List
和 ListItem
组件来创建导航菜单:import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
// ...
<Drawer
className={classes.drawer}
variant="permanent"
classes={{
paper: classes.drawerPaper,
}}
>
<List>
<ListItem button>
<ListItemText primary="菜单项1" />
</ListItem>
<ListItem button>
<ListItemText primary="菜单项2" />
</ListItem>
{/* 添加更多菜单项 */}
</List>
</Drawer>
这样,你就可以将 Material-UI 应用程序的抽屉放在页眉下面了。根据实际需求,你可以自定义样式和添加更多的菜单项。
领取专属 10元无门槛券
手把手带您无忧上云