Map在vue中的使用方法:
html:遍历的时候要遍历两遍
<template>
<div class="course">
<div class="">
<div class="pt0 ctRow">
<ul class="ctNavbar">
<li class="ctA"
v-for="(item,index) in courseTypeList"
:key="item.id"
@click="selectedCur(index)" :class="{ cur:cur == index }"
:data-id="item.id"
>
{{item.courseTypeName}}
</li>
</ul>
</div>
<!--Map遍历两遍 -->
<div class="ctRow date_navbar">
<a href="javascript:;"
v-for="(item,index) in dateMap"
:key="index"
@click="dataCur(index)" :class="{ cur:active == index }"
>
<span v-for="data in item">{{data}}</span>
</a>
</div>
</div>
</div>
</template>
js:
data(){
return{
cur: "0",
active:"0",
courseTypeList: [],
dateMap:{},
}
},
mounted(){
// 后端返回json
var jsonStr ={
"code": 0,
"msg": "success",
"data": {
"courseTypeList": [
{
"id": "16",
"courseTypeName": "小一班"
},
{
"id": "15",
"courseTypeName": "中一班"
},
{
"id": "14",
"courseTypeName": "大一班"
}
],
"dateKeys": [
"二·11.26",
"三·11.27",
"四·11.28",
"五·11.29",
"六·11.30",
"日·12.01",
"一·12.02"
]
}
}
// 遍历班级类型
for (var i = 0; i < jsonStr.data.courseTypeList.length; i++) {
var courseTypeList = jsonStr.data.courseTypeList[i];
this.courseTypeList.push(courseTypeList)
}
// 遍历日期
//初始化Map对象
var dateMap = new Map();
for (var i = 0; i < jsonStr.data.dateKeys.length; i++) {
var data = jsonStr.data.dateKeys[i];
//用split连在一起的字符串切割 "三·11.27"
//使用set添加键值示例:m.set('小红', 30);
dateMap.set(data.split("·")[0], data.split("·")[1]);
}
this.dateMap = dateMap;
},
css:
.ctNavbar{
display: flex;
align-items: center;
justify-content: space-between;
text-align: center;
.ctA{
padding: 10px 0;
color: #202020;
font-size: 1.5rem !important;
border-bottom: 2px solid transparent;
width: 33%;
&.cur{
color: #BA0932;
border-bottom: 2px solid #BA0932;
}
}
}
.date_navbar {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
a {
width: 32px;
-webkit-box-flex: 1;
display: flex;
justify-content: space-between;
align-items: center;
flex-direction: column;
font-size: 9px;
color: #6C6C6C;
padding: 2px 5px;
flex: 1;
span {
display: block;
-webkit-box-flex: 1;
}
&.cur {
background: #BA0932;
color: #fff;
border-radius: 16px;
}
}
}