我正在玩物质ui。我使用路由实现了LeftNav,但我找不到获取IconMenu的方法,或者菜单使用链接或路由。任何人都可以指点我一个好的来源/教程?文档不足,两个组件似乎都不支持'menuItems'作为LeftNav的属性。
答案 0 :(得分:53)
2016年12月编辑: linkButton
道具已弃用,您会收到警告:
Warning: Unknown props `linkButton` on <a> tag.
所以简单地删除道具:
<MenuItem
containerElement={<Link to="/profile" />}
primaryText="Profile"
leftIcon={
<FontIcon className="material-icons">people</FontIcon>
}
/>
Here's an Example Repo和the Live Demo Here。
原始答案:
只是想指出,如果您使用的是react-router,则可能需要使用<Link to="/some/page" />
而不是<a>
标记。
为此,您需要使用containerElement
道具
<MenuItem
linkButton
containerElement={<Link to="/profile" />}
primaryText="Profile"
leftIcon={
<FontIcon className="material-icons">people</FontIcon>
}
/>
(如果您只是传递={true}
,则true
可以省略,
换句话说,<MenuItem linkButton />
与<MenuItem linkButton={true} />
)
containerElement
和linkButton
道具实际上已记录in the buttons section,但您也可以在MenuItem
中使用它。
答案 1 :(得分:12)
您可以在linkButtton
上设置MenuItem
支柱以生成链接,然后添加href
。
<MenuItem linkButton={true} href="link/to/some/page" primaryText="Sample Link" />
答案 2 :(得分:6)
linkButton
的道具EnhancedButton
已被弃用。提供href
属性时不再需要LinkButton。
它将在v0.16.0中删除。
<MenuItem onTouchTap={this.handleClose} href="/link/data">Link Item</MenuItem>
工作正常
答案 3 :(得分:4)
在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
组件。)
答案 4 :(得分:2)
(2018年9月)的现有答案中没有一个对我来说适用于react 16.4.2和react-router 4.2.2,所以这是我的解决方案:
<Link to='/notifications' style={{ textDecoration: 'none' }}>
<MenuItem>Notifications</MenuItem>
</Link>
如您所见,MenuItem组件被Link组件包围,并且我添加了一个样式textDecoration:无,不带下划线。
答案 5 :(得分:1)
经过一些自我搜索之后,我得到了一个略有不同的解决方案:
<MenuItem onClick={() => {handleClose("/#about")}}>About me</MenuItem>
具有支持的JS功能:
function handleClose(nav) {
window.location.href = nav;
}
希望能证明它是替代品。
答案 6 :(得分:1)
您可以先使用 react-route-dom 和MenuItem onClick 属性导入import-router-dom:
import { useHistory } from 'react-router-dom'
然后在组件中声明一个函数来处理onClick:
const navigate = () => history.push('route/to/navigate')
,然后使用MenuItem
<MenuItem onClick={navigate}>Navigate</MenuItem>
答案 7 :(得分:1)
截至2020年9月,唯一可行且超级简单的是
<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>
答案 8 :(得分:0)
onTouchTap对我不起作用我必须使用onClick查看下面的示例
<MenuItem
onClick={this.logout}
containerElement={<Link to="/" />}
primaryText="Sign out"
rightIcon={
<b style={style.rightIcon}>
<img
className="menu-img"
src="img/app/logout.png"
alt="logout"
/>
</b>
}
/>
希望这也有助于其他人
答案 9 :(得分:0)
使用M-UI 4.x,您可以在component
上将MenuItem
道具设置为类似
<MenuItem component='a' href='link/to/some/page'>Sample Link</MenuItem>
答案 10 :(得分:-1)
这是我的实现,看起来与material-ui官方网站中的内容完全相同。您可以使用的组件包括true && { echo; } # works
,AppBar
和Drawer
。 ListItem
可以实现为SelectableList
。
let SelectableList = MakeSelectable(List)
答案 11 :(得分:-1)
我无法使用containerElement
在iOS上使react-router
工作。我正在使用材质用户界面0.17.2
和react-router@v3
,这是一个适用于我的工作:
<MenuItem
onTouchTap={() => {
this._yourMethod()
browserHistory.push('/howItWorks')
}}
primaryText="Menu Link"
/>