react實(shí)現(xiàn)菜單權(quán)限控制的方法
通常公司的后臺管理系統(tǒng)都需要權(quán)限控制,即不同的角色用戶看到不同的菜單,如下圖:

下面,通過react實(shí)現(xiàn)這樣的后臺管理系統(tǒng)(腳手架),功能簡介:
1.頂部的菜單項(xiàng)根據(jù)用戶的角色動態(tài)生成。
2.側(cè)邊測菜單項(xiàng)根據(jù)已選的頂部菜單動態(tài)生成。
直接上代碼:
路由配置:
export default (
<Route path="/" component={App}>
<IndexRoute component={EmployeeList}/>
<Route path="/employee" component={Employee}>
<IndexRoute component={EmployeeList}/>
<Route path="/employee/list" component={EmployeeList}/>
<Route path="/employee/detail/:id" component={EmployeeDetail}/>
</Route>
<Route path="/goods" component={Goods}>
<IndexRoute component={GoodsList}/>
<Route path="/goods/list" component={GoodsList}/>
<Route path="/goods/detail/:id" component={GoodsDetail}/>
</Route>
</Route>
)
頂部菜單項(xiàng)單獨(dú)成了一個組件:
// 動態(tài)數(shù)據(jù)
import React, { Component } from 'react'
import { Link } from 'react-router' // 引入Link處理導(dǎo)航跳轉(zhuǎn)
import { connect } from 'react-redux'
import { fetchPostsIfNeeded, updateSubMenuWhenClick } from '../actions/count'
import { Menu } from 'antd';
class TopMenu extends Component {
constructor(props){
super(props);
this.handleMenuClick = this.handleMenuClick.bind(this);
}
handleMenuClick(e){
// console.log(e.item.props['data-menukey']);
const { updateSubMenuWhenClick } = this.props
updateSubMenuWhenClick(true, e.item.props['data-menukey'])
}
componentWillMount() {
}
componentDidMount() {
const { fetchPostsIfNeeded } = this.props
fetchPostsIfNeeded()
}
render() {
const { menuList, fetchPostsIfNeeded } = this.props
if(menuList.length != 0) {
fetchPostsIfNeeded(true, menuList[0].key)
}
return (
<Menu
theme="dark"
mode="horizontal"
defaultSelectedKeys={['0']}
style={{ lineHeight: '64px' }}
onClick={this.handleMenuClick}
>
{
menuList.map((e, index) =>
<Menu.Item key={index} data-menukey={e.key} >
<Link to={{ pathname: e.url }} >{e.name}</Link>
</Menu.Item>
)
}
</Menu>
)
}
}
const getList = state => {
return {
menuList: state.update.menuList
}
}
export default connect(
getList,
{ fetchPostsIfNeeded, updateSubMenuWhenClick }
)(TopMenu)
在render函數(shù)中,如果動態(tài)生成的頂部菜單數(shù)據(jù)長度不為0,則根據(jù)頂部菜單的key動態(tài)生成側(cè)邊菜單項(xiàng)。
const { menuList, fetchPostsIfNeeded } = this.props
if(menuList.length != 0) {
fetchPostsIfNeeded(true, menuList[0].key)
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
可定制React自動完成搜索組件Turnstone實(shí)現(xiàn)示例
這篇文章主要為大家介紹了可定制React自動完成搜索組件Turnstone實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
React Native 搭建開發(fā)環(huán)境的方法步驟
本篇文章主要介紹了React Native 搭建開發(fā)環(huán)境的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
react使用CSS實(shí)現(xiàn)react動畫功能示例
這篇文章主要介紹了react使用CSS實(shí)現(xiàn)react動畫功能,結(jié)合實(shí)例形式分析了react使用CSS實(shí)現(xiàn)react動畫功能具體步驟與實(shí)現(xiàn)方法,需要的朋友可以參考下2020-05-05
react?Table準(zhǔn)備Spin?Empty?ConfigProvider組件實(shí)現(xiàn)
這篇文章主要為大家介紹了react?Table準(zhǔn)備Spin、Empty、ConfigProvider組件實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-02-02
基于CSS實(shí)現(xiàn)MaterialUI按鈕點(diǎn)擊動畫并封裝成 React 組件
筆者先后開發(fā)過基于vue,react,angular等框架的項(xiàng)目,碧如vue生態(tài)的elementUI, ant-design-vue, iView等成熟的UI框架, react生態(tài)的ant-design, materialUI等,這些第三方UI框架極大的降低了我們開發(fā)一個項(xiàng)目的成本和復(fù)雜度,使開發(fā)者更專注于實(shí)現(xiàn)業(yè)務(wù)邏輯和服務(wù)化2021-11-11
react版模擬亞馬遜人機(jī)交互菜單的實(shí)現(xiàn)
本文主要介紹了react版模擬亞馬遜人機(jī)交互菜單的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
React Hooks獲取數(shù)據(jù)實(shí)現(xiàn)方法介紹
這篇文章主要介紹了react hooks獲取數(shù)據(jù),文中給大家介紹了useState dispatch函數(shù)如何與其使用的Function Component進(jìn)行綁定,實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

