VUE--城市列表

在这里插入图片描述
vant库地址:https://vant-contrib.gitee.io/vant/v3/#/zh-CN

<template>
  <div>
    <van-index-bar :index-list="computedCityList" @select="handleSelect">
        <div v-for="data in cityList" :key="data.type">
            <van-index-anchor :index="data.type" />
            <van-cell :title="item.name" v-for="(item,index) in data.list" :key="index" @click="handleChangePage(item.name,item.cityId)"/>
        </div>
    </van-index-bar>
  </div>
</template>
<script>
import http from '@/util/http'
import Vue from "vue";
import { IndexBar, IndexAnchor,Cell, Toast } from "vant";
import {mapMutations} from 'vuex'

Vue.use(IndexBar);
Vue.use(IndexAnchor).use(Cell);

export default {
    data(){
        return{
            cityList:[]
        }
    },
    computed:{
        computedCityList(){
            return this.cityList.map(item=>item.type)
        }
    },
    mounted(){
        http({
            url:`/gateway?k=5619478`,
            headers:{
                'X-Host': 'mall.film-ticket.city.list'
            }
        }).then(res=>{
            // console.log(res);
            this.cityList=this.handleCityDatga(res.data.data.cities)
            console.log(this.cityList);
        })
    },
    methods:{
        ...mapMutations('CityModule',['changeCityName','changeCityId']),
        handleCityDatga(cities){
            let letterArr=[]
            let newCities=[]
            for(let code=65;code<91;code++){
                letterArr.push(String.fromCharCode(code))
            }
            letterArr.forEach((letter)=>{
                let list=cities.filter(item=>item.pinyin.substring(0,1).toUpperCase()===letter)
                // console.log(list);
                if(list.length>0){
                    newCities.push({
                        type:letter,
                        list:list
                    })
                }
            })
            // console.log(newCities);
            return newCities
        },
        handleSelect(index){
            Toast(index)
        },
        handleChangePage(cityName,cityId){
            // this.$store.state.cityName=cityName
            // this.$store.commit('changeCityName',cityName)
            // this.$store.commit('changeCityId',cityId)
            this.changeCityName(cityName)
            this.changeCityId(cityId)
            this.$router.back()
        },
    }
};
</script>

项目源码:
链接:https://pan.baidu.com/s/1K8HnVEZy87O6-gl6qsZnxw
提取码:t70u
复制这段内容后打开百度网盘手机App,操作更方便哦

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值