• vant做城市列表


    vant: https://youzan.github.io/vant/#/zh-CN/

     安装

    cnpm i -S vant

    按需加载配置

    #  babel.config.js 中配置

     

    module.exports = {
      plugins: [
        ['import', {
          libraryName: 'vant',
          libraryDirectory: 'es',
          style: true
        }, 'vant']
      ],
      presets: [
        '@vue/cli-plugin-babel/preset'
      ]
    }

     

    对于获取到的城市数据进行处理创建个api.js文件

    
    
     // 引入封装头信息和请求域名的axios对象
    import http from './http'
     // 引入请求的url地址
    import{
      cityUri
    } from '../config/uri'
    export const cityData = async () => {
      // 判断本地是否有缓存,如果有则不请求远程数据
      let cacheData = !localStorage.getItem('cityData') ? [] : JSON.parse(localStorage.getItem('cityData'))
      if(cacheData.length > 0){
        return Promise.resolve(cacheData)
      }
      let ret = await http.get(cityUrl,{
        headers:{
          // 由于请求头信息中不同的需求不同的请求头,所以要判断所用的条件
          'info':'city'
        }
      })
      let retData = ret.data.cities
      // 城市字母索引
      let cityIndex = []
      // 处理完成后的数据
      let dataList = []
      let indexList = []
      for(let i = 65 ; i <= 90 ; i++){
        // 这是String.fromCharCode的示例 可接受一个指定的 Unicode 值,然后返回一个字符串
        // document.write(String.fromCharCode(65,66,67) )   输出 ABC 
        cityIndex.push(String.fromCharCode(i))
      }
      cityIndex.forEach(index => {
        let tmpArr = retData.filter(item => index.toLowerCase() == item.pinyin.substr(0,1))
        if(tmpArr.length > 0){
          indexList.push(index)
          dataList.push({
            index,
            data:tmpArr
          })
        }
      })
      let data = [dataList,indexList]
      localStorage.setItem('cityData',JSON.stringify(data))
      return Promise.resolve([dataList,indexList])
      }

    在组件中使用

    <template>
      <div>
        <van-index-bar :index-list="indexList" highlight-color="#ff0000">
          <template v-for="item in datalist">
            <van-index-anchor :index="item.index" :key="item.index" />
            <van-cell v-for="subitem in item.data" :title="subitem.name" />
          </template>
        </van-index-bar>
      </div>
    </template>

    <script>
    import Vue from 'vue'
    import { IndexBar, IndexAnchor, Cell } from 'vant'
    import { cityData } from '../../api/api'
    Vue.use(IndexBar)
    Vue.use(IndexAnchor)
    Vue.use(Cell)
    export default {
      data() {
        return {
          dataList: [],
          indexList: ['A', 'B']
        }
      },
      mounted() {
        this.getData()
      },
      methods: {
        async getData() {
          const ret = await cityData()
          this.datalist = ret[0]
          this.indexList = ret[1]
        }
      }
    }
    </script>

    <style></style>

     

    右侧打赏一下 代码改变世界一块二块也是爱
  • 相关阅读:
    如果使用EntityFramework6链接Mysql
    MongoDB联合查询 -摘自网络
    “TableDetails”中列“IsPrimaryKey”的值为DBNull. Mysql EntityFramework
    使用NPOI 转换Excel TO HTML (导出格式不如原生Excel好看)
    如何使用ODBC搭配dsn链接数据库
    Ubuntu16.04安装配置sublime text3
    ubuntu16.04编译安装php7.2
    ubuntu16.04安装flash player与谷歌浏览器(chrome)
    ubuntu16编译安装mysql5.7
    phpstorm+wamp+xdebug配置php调试环境
  • 原文地址:https://www.cnblogs.com/ht955/p/14271061.html
Copyright © 2020-2023  润新知