1. 程式人生 > >【面向對象】關於利用數組對象的創建底部欄

【面向對象】關於利用數組對象的創建底部欄

個數 () get .com 管理 mount xtend click mon

/*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 } />
</div>
<p>{i.font}</p>
</div>
)}

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

};

}

這樣子後期修改/增加底部欄的個數 只需要在數組上做修改即可,包括後臺對權限的管理,靈活性更大

【面向對象】關於利用數組對象的創建底部欄