遞歸樹法,react 樹形菜單_react使用antd組件遞歸實現左側菜單導航樹

 2023-10-05 阅读 22 评论 0

摘要:/** 左側導航組件*/import React, { Component }from 'react'import { Link, withRouter }from 'react-router-dom'/**react自行傳入路由參數---withRouter組件*/import { Layout, Menu, Button, Icon }from 'antd';import {MenuUnfoldOutlined,MenuF

/** 左側導航組件*/import React, { Component }from 'react'import { Link, withRouter }from 'react-router-dom'/**react自行傳入路由參數---withRouter組件*/import { Layout, Menu, Button, Icon }from 'antd';

import {

MenuUnfoldOutlined,

MenuFoldOutlined,

HomeOutlined,

遞歸樹法?ShopOutlined,

SettingOutlined,

UserOutlined,

AppstoreOutlined

}from '@ant-design/icons';

import'./index.less'import LogoImgfrom '../../assets/imgs/logo.jpg'

react路由切換組件側導航?/** 導航配置信息列表(這里我們可以導入命名為menuConfigList) */

import menuConfigList from '../../config/menuConfig'/** 注意:export default默認暴露的可以命名為任意值 */

/** Antd布局組件*/

const { Sider } =Layout;const { SubMenu } =Menu;classLeftNav extends Component {

constructor(props) {

super(props);this.state ={

react重新加載組件、collapsed:false,

h2Opacity:true}

}

toggleCollapsed= () =>{

console.log(this.state.collapsed)this.setState({

collapsed:!this.state.collapsed,

react 自定義組件?h2Opacity: Number(!this.state.h2Opacity)

})

}

render() {const defaultSelectedKeysPathname = this.props.location.pathname.slice(1)return(

后臺首頁

后臺管理

{React.createElement(this.state.collapsed ?MenuUnfoldOutlined : MenuFoldOutlined)}

defaultSelectedKeys={[defaultSelectedKeysPathname]} /** 初始展開的 SubMenu 菜單項 key 數組*/defaultOpenKeys={['sub1']}/** 初始選中的菜單項 key 數組*/mode="inline"theme="dark"

react框架?>{ this.getMenuNodes(menuConfigList) }{/* 后期涉及權限,所以這里采用遍歷+遞歸 */}

)

}

getMenuNodes= (data) => {

return data.map( item => {

if(!item.children){

return (

遞歸遍歷,{item.title}

)

}else{

return (

key={item.menu_path}

遞歸、title={

{item.title}

}

>

{ this.getMenuNodes(item.children) }{/* 遞歸調用,渲染二級列表 */}

)

}

react grid。})

}

}

exportdefault withRouter(LeftNav);

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/115030.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息