【发布时间】:2021-10-07 22:31:45
【问题描述】:
我正在尝试在我的项目中实现 Layout 的标题
“错误:失败的道具类型:道具href 在<Link> 中需要string 或object,但得到了undefined。
打开浏览器的控制台以查看组件堆栈跟踪。”
你能指导我做错什么吗?
import {
Box,
Button,
Divider,
List,
ListItem,
ListItemText,
Menu,
MenuItem
} from '@material-ui/core';
import { useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import Link from 'next/link';
import { styled } from '@material-ui/core/styles';
import ExpandMoreTwoToneIcon from '@material-ui/icons/ExpandMoreTwoTone';
const ListWrapper = styled(Box)(
({ theme }) => `
.MuiTouchRipple-root {
display: none;
}
.MuiListItem-root {
transition: ${theme.transitions.create(['color', 'fill'])};
&.MuiListItem-indicators {
padding: ${theme.spacing(1, 2)};
.MuiListItemText-root {
.MuiTypography-root {
&:before {
height: 4px;
width: 22px;
opacity: 0;
visibility: hidden;
display: block;
position: absolute;
bottom: -10px;
transition: all .2s;
border-radius: ${theme.general.borderRadiusLg};
content: "";
background: ${theme.colors.primary.main};
}
}
}
&.active,
&:active,
&:hover {
background: transparent;
.MuiListItemText-root {
.MuiTypography-root {
&:before {
opacity: 1;
visibility: visible;
bottom: 0px;
}
}
}
}
}
}
`
);
function HeaderMenu() {
const { t } = useTranslation();
const ref = useRef(null);
const [isOpen, setOpen] = useState(false);
const handleOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<>
<ListWrapper>
<List component={Box} display="flex">
<ListItem
classes={{ root: 'MuiListItem-indicators' }}
button
component={Link}
to="/preview/management/users/list"
>
<ListItemText
primaryTypographyProps={{ noWrap: true }}
primary={t('Users')}
/>
</ListItem>
<ListItem
classes={{ root: 'MuiListItem-indicators' }}
button
component={Link}
to="/preview/dashboards/banking"
>
<ListItemText
primaryTypographyProps={{ noWrap: true }}
primary={t('Banking')}
/>
</ListItem>
<ListItem
classes={{ root: 'MuiListItem-indicators' }}
button
ref={ref}
onClick={handleOpen}
>
<ListItemText
primaryTypographyProps={{ noWrap: true }}
primary={
<Box display="flex" alignItems="center">
{t('Help')}
<Box display="flex" alignItems="center" pl={0.3}>
<ExpandMoreTwoToneIcon fontSize="small" />
</Box>
</Box>
}
/>
</ListItem>
</List>
</ListWrapper>
<Menu anchorEl={ref.current} onClose={handleClose} open={isOpen}>
<MenuItem component={Link} to="/overview">
{t('Features tour')}
</MenuItem>
<MenuItem component={Link} to="/docs/introduction">
{t('Getting started guide')}
</MenuItem>
<MenuItem component={Link} to="/docs/contact-support">
{t('Contact support')}
</MenuItem>
<Divider sx={{ my: 1.5 }} />
<Button
variant="contained"
fullWidth
href="https://material-ui.com/store/items/tokyo-react-admin-dashboard"
target="_blank"
rel="noopener"
>
{t('Buy now')}
</Button>
</Menu>
</>
);
}
export default HeaderMenu;
【问题讨论】:
-
尝试使用 href 而不是