【面向对象】关于利用数组对象的创建底部栏

本文介绍了一个使用React实现的灵活底部导航栏组件,通过数组配置管理菜单项,支持动态修改和权限控制,增强了组件的可维护性和扩展性。
/*eslint-disable*/
//底部导航栏组件
import React from "react";
import {render} from "react-dom";
import "./compoments.less";
import indexNo from '../images/index-no.png'
import indexYes from '../images/index-yes.png'
import shopNo from '../images/shop-no.png'
import shopYes from '../images/shop-yes.png'
import serviceNo from '../images/server-no.png'
import serviceYes from '../images/server-yes.png'
import myNo from '../images/my-no.png'
import myYes from '../images/my-yes.png'

export default class BottomBar extends React.Component {
constructor(props) {
super(props);
}

state = {
bottomBar: [{
name: "index",
isActive: false,
icon: indexNo,
activeIcon: indexYes,
font: "首页",
url: "#/index"
}, {
name: "shop",
isActive: false,
icon: shopNo,
activeIcon: shopYes,
font: "门店",
url: "#/index"
}, {
name: "service",
isActive: false,
icon: serviceNo,
activeIcon: serviceYes,
font: "门店",
url: "#/service"
}, {
name: "my",
isActive: false,
icon: myNo,
activeIcon: myYes,
font: "我的",
url: "#/my"
}]
};

/**
* 页面开始获取哪个菜单属于被点击状态
*/
componentWillMount() {
let _select = localStorage.getItem('select');
let {bottomBar} = this.state;
bottomBar.filter(item => {
if (item.name == _select) {
item.isActive = true
}
});
this.setState(
bottomBar
);
}

setItem(_item) {
localStorage.setItem('select', _item.name);
location.replace(_item.url);
}

render() {
const BottomBar = ()=> {
let {bottomBar} = this.state;
return (
<div className="tabBar">
{ bottomBar.map((i) =>
<div id={i.name} className="bottom-common-line-div index" key={i.name}
onClick={v => this.setItem(i,v)}>
<div className="icon-img">
<img src={i.isActive == true ? i.activeIcon : i.icon } alt=""/>
</div>
<p>{i.font}</p>
</div>
)}

</div>
)
};
return (<BottomBar/>)

};

}

这样子后期修改/增加底部栏的个数 只需要在数组上做修改即可,包括后台对权限的管理,灵活性更大

转载于:https://www.cnblogs.com/xyyyy/p/10273587.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值