我正在使用material-ui。我使用routes实现了LeftNav,但是我找不到一种方法来使IconMenu或者Menu与链接或路由一起工作。有人可以指点我一个好的资源/教程吗?文档不够详细,而且这两个组件似乎都不支持像LeftNav那样的'menuItems'属性。
我正在使用material-ui。我使用routes实现了LeftNav,但是我找不到一种方法来使IconMenu或者Menu与链接或路由一起工作。有人可以指点我一个好的资源/教程吗?文档不够详细,而且这两个组件似乎都不支持像LeftNav那样的'menuItems'属性。
又是一个早该更新的内容:
containerElement
属性不再被支持,改用 component
属性。
在这里查看文档。
2016 年 12 月编辑:linkButton
属性已弃用,您将会收到一个警告:
Warning: Unknown props `linkButton` on <a> tag.
因此,只需删除该属性:
<MenuItem
containerElement={<Link to="/profile" />}
primaryText="Profile"
leftIcon={
<FontIcon className="material-icons">people</FontIcon>
}
/>
Translated answer:
我想指出,如果你正在使用 react-router,你可能需要使用 <Link to="/some/page" />
而不是 <a>
标签。
为了实现这一点,你需要使用 containerElement
属性。
<MenuItem
linkButton
containerElement={<Link to="/profile" />}
primaryText="Profile"
leftIcon={
<FontIcon className="material-icons">people</FontIcon>
}
/>
如果您只传递true
,也就是说,<MenuItem linkButton />
和<MenuItem linkButton={true} />
是一样的,因此可以省略={true}
。
containerElement
和 linkButton
属性实际上在按钮部分有文档记录,但是您也可以在MenuItem
中使用它们。
从 Material-UI 1.0 开始,新的语法为:
<MenuItem
component={Link}
// the 'to' prop (and any other props not recognized by MenuItem itself)
// will be passed down to the Link component
to="/profile"
>
Profile
</MenuItem>
(注:此示例不包括图标。有一个新的ListItemIcon
组件可以使用。)
ListItem
。 - piotroscomponent={()=><Link to='/profile'/>}
- ICWconst menuItems: MenuItemProps[] = [{id: 'someId', children: 'Go', component={Link}}]
并且看到错误 - Type '{ id: string; onClick: () => void; children: string; component: string; }' is not assignable to type 'MenuItemProps<"li", {}>'. Object literal may only specify known properties, and 'component' does not exist in type 'MenuItemProps<"li", {}>'.
. 它有什么问题? - lazy.lizardMenuItem
上设置 linkButtton
属性来生成链接,然后还可以添加一个 href
。<MenuItem linkButton={true} href="link/to/some/page" primaryText="Sample Link" />
在现有答案中(截至2018年9月),没有适用于我使用的react 16.4.2和react-router 4.2.2,所以这是我的解决方案:
<Link to='/notifications' style={{ textDecoration: 'none' }}>
<MenuItem>Notifications</MenuItem>
</Link>
如您所见,MenuItem 组件被 Link 组件包围,并且我添加了一个样式 textDecoration: 'none'
以使该项目不带有下划线。
outline: "none"
。 - CorayThanli
放在 a
里面,在语义上没有意义。 - DanV<MenuItem component="a" href="/your-path">Click Me</MenuItem>
奖励:添加带有徽章的图标:
<MenuItem component="a" href="/your-path">
<IconButton color="inherit" href="/your-path">
<Badge badgeContent="12" color="secondary">
<StarsSharpIcon color="action"/>
</Badge>
</IconButton>
Click Me
</MenuItem>
a
会导致页面重新加载。 - piotrosEnhancedButton
的属性linkButton
已经被弃用。当提供href
属性时,不再需要LinkButton。
它将在v0.16.0中被移除。
<MenuItem onTouchTap={this.handleClose} href="/link/data">Link Item</MenuItem>
运行正常
target="_blank"
等内容来在新的浏览器窗口中打开链接。 - Chris SpranceLink
的元素不会。相反,可以简单地将 MenuItem
包装起来:<Link to='/'><MenuItem>主页</MenuItem></Link>
- keepthepeachcomponent
的属性,并将其设置为某些内容。<MenuItem component='a' href='link/to/some/page'>Sample Link</MenuItem>
a
会导致页面重新加载。 - piotrosa
导致页面重新加载的含义是什么? - jkschinimport {Menu, MenuItem, Link} from '@mui/material'
<Menu>
<MenuItem component={Link} href='/'>Home</MenuItem>
<MenuItem component={Link} href='/settings/user'>My Settings</MenuItem>
<Menu />
Link
作为MenuItem
的component
属性传递,将使用Link
作为该MenuItem
的根元素。有关您可以使用Link
元素执行的其他操作的文档可以在此处找到:这里。从 v5.2.2 版本开始,您可以使用以下内容:
import { MenuItem } from '@mui/material';
import { Link } from 'react-router-dom';
<MenuItem component={Link} to="/profile">
Profile
</MenuItem>
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import { Link } from 'react-router-dom';
<MenuItem component={Link} to='/login'>
Login
</MenuItem>
对于我来说,这在2021年9月使用React Router和MenuItem的链接属性非常有效。
onTouchTap
/onClick
触发页面更改而不是<a>
/<Link />
,您将失去一些本机功能,例如浏览器中的链接预览以及用户在新标签页中打开链接的能力。 - DaxChen