Home  >  Article  >  Web Front-end  >  How to implement menu permission control using react

How to implement menu permission control using react

亚连
亚连Original
2018-06-21 14:35:262391browse

This article mainly introduces the method of implementing menu permission control in react. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.

Usually the company’s backend management system requires permission control, that is, users with different roles see different menus, as shown below:

Below, such a backend management system (scaffolding) is implemented through react. Function introduction:

1. The menu items at the top are dynamically generated based on the user's role.

2. Side test menu items are dynamically generated based on the selected top menu.

Go directly to the code:

Routing configuration:

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>
)

The top menu item becomes a separate component:

// 动态数据
import React, { Component } from &#39;react&#39;
import { Link } from &#39;react-router&#39; // 引入Link处理导航跳转
import { connect } from &#39;react-redux&#39;
import { fetchPostsIfNeeded, updateSubMenuWhenClick } from &#39;../actions/count&#39;
import { Menu } from &#39;antd&#39;;
class TopMenu extends Component {
  constructor(props){
    super(props);
    this.handleMenuClick = this.handleMenuClick.bind(this);
  }

  handleMenuClick(e){
    // console.log(e.item.props[&#39;data-menukey&#39;]);
    const { updateSubMenuWhenClick } = this.props
    updateSubMenuWhenClick(true, e.item.props[&#39;data-menukey&#39;])
  }
  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={[&#39;0&#39;]}
        style={{ lineHeight: &#39;64px&#39; }}
        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)

In the render function, if it is dynamically generated The data length of the top menu is not 0, and the side menu items are dynamically generated based on the key of the top menu.

const { menuList, fetchPostsIfNeeded } = this.props
    if(menuList.length != 0) {
      fetchPostsIfNeeded(true, menuList[0].key)
    }

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement multi-page development in webpack

##How to implement the sliding menu component in Vue

How to implement 3D cinema using three.js

About function throttling and function anti-shake in JS (detailed tutorial)

The above is the detailed content of How to implement menu permission control using react. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn