vue遍历Map,Map在vue中的使用方法

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;
            }
        }
    }
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值