vue3写省市镇三级联动组件

<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:isShow}">
      <span v-if='!fullLocation' class="placeholder">请选择配送地址span>
      <span v-else class="value">{{fullLocation}}span>
      <i class="iconfont icon-angle-down">i>
    div>
    <div class="option" v-show='isShow'>
      <div class="loading" v-if="loading">div>
      <template v-else>
        <span @click="changeCity(item)" class="ellipsis" v-for="item in cityList" :key="item.code">{{item.name}}span>
      template>
    div>
  div>
template>

<script>
import { ref, computed, reactive } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getCityList } from '@/api/product.js'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup(props, { emit }) {
    const isShow = ref(false)
    const loading = ref(false)
    // 控制选择城市弹窗的显示和隐藏
    const toggle = () => {
      isShow.value = !isShow.value
      if (isShow.value) {
        loading.value = true
        // 调用接口之前,把之前选中的数据置空
        for (const key in changeResult) {
          changeResult[key] = ''
        }
        // 弹层显示了,调用接口
        getCityList().then(ret => {
          list.value = ret
          loading.value = false
        })
      }
    }

    const target = ref(null)
    onClickOutside(target, () => {
      isShow.value = false
    })

    // 城市列表原始数据
    const list = ref([])

    // 选中的省市区
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })

    // 选择城市操作
    const changeCity = city => {
      if (city.level === 0) {
        // 点击的省级单位
        changeResult.provinceCode = city.code
        changeResult.provinceName = city.name
      } else if (city.level === 1) {
        // 点击的市级单位
        changeResult.cityCode = city.code
        changeResult.cityName = city.name
      } else if (city.level === 2) {
        // 点击的县级单位:选中最终的省市区数据,并且传递给父组件
        changeResult.countyCode = city.code
        changeResult.countyName = city.name
        // 组合完整的省市区名称
        changeResult.fullLocation = `${changeResult.provinceName}${changeResult.cityName}${changeResult.countyName}`
        // 关闭碳层
        isShow.value = false
        // 把选中的数据最终传递给父组件
        emit('change-city', changeResult)
      }
    }

    // 通过计算属性计算当前显示的列表数据:省级;市级;县级
    const cityList = computed(() => {
      let result = list.value
      // 当前点击的是省,那么就计算市级列表
      if (changeResult.provinceCode && changeResult.provinceName) {
        result = result.find(item => item.code === changeResult.provinceCode).areaList
      }
      // 当前点击的是市,那么就计算县级列表
      if (changeResult.cityCode && changeResult.cityName) {
        result = result.find(item => item.code === changeResult.cityCode).areaList
      }
      return result
    })

    return { isShow, toggle, target, cityList, loading, changeCity }
  }
}
script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
style>

省市镇获取接口

// 省市区列表数据的请求路径
 const cityURL = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
/ 获取省市区列表数据
// 添加缓存功能,存储到window中
export const getCityList = async () => {
  if (window.cityList) {
    return window.cityList
  }
  const ret = await axios.get(cityURL)
  if (ret.data) {
    window.cityList = ret.data
  }
  return ret.data
}

在父组件中使用

<template>
  <p class="g-name">{{goods.name}}p>
  <p class="g-desc">{{goods.desc}}p>
  <p class="g-price">
    <span>{{goods.price}}span>
    <span>{{goods.oldPrice}}span>
  p>
  <div class="g-service">
    <dl>
      <dt>促销dt>
      <dd>12月好物放送,App领券购买直降120元dd>
    dl>
    <dl>
      <dt>配送dt>
      <dd>dd>
      <dd>
      <XtxCity @change-city='changeCity' :fullLocation='fullLocation' />
      dd>
    dl>
    <dl>
      <dt>服务dt>
      <dd>
        <span>无忧退货span>
        <span>快速退款span>
        <span>免费包邮span>
        <a href="javascript:;">了解详情a>
      dd>
    dl>
  div>
template>

<script>
import { toRef, ref } from 'vue'
export default {
  name: 'GoodName',
  props: {
    goods: {
      type: Object,
      default: () => {}
    }
  },
  setup(props) {
    // 实现默认地址的获取
    // 如果有默认地址就获取默认即可,如果没有默认地址就获取第一个地址
    const goods = toRef(props, 'goods')
    const provinceCode = ref('110000')
    const cityCode = ref('119900')
    const countyCode = ref('110101')
    const fullLocation = ref('北京市 市辖区 东城区')

    if (goods.value.userAddresses && goods.value.userAddresses.value.length) {
      const defaultAddress = goods.value.userAddresses.value.find(item => item.isDefault === 1)
      if (defaultAddress) {
        // 获取默认地址
        provinceCode.value = defaultAddress.provinceCode
        cityCode.value = defaultAddress.cityCode
        countyCode.value = defaultAddress.countyCode
        fullLocation.value = defaultAddress.fullLocation
      } else {
        // 获取第一项地址
        provinceCode.value = goods.value.userAddresses.value[0].provinceCode
        cityCode.value = goods.value.userAddresses.value[0].cityCode
        countyCode.value = goods.value.userAddresses.value[0].countyCode
        fullLocation.value = goods.value.userAddresses.value[0].fullLocation
      }
    }

    // 更新选中的省市区数据
    const changeCity = cityInfo => {
      provinceCode.value = cityInfo.provinceCode
      cityCode.value = cityInfo.cityCode
      countyCode.value = cityInfo.countyCode
      fullLocation.value = cityInfo.fullLocation
    }

    return { fullLocation, changeCity }
  }
}
script>

<style lang="less" scoped>
.g-name {
  font-size: 22px;
}
.g-desc {
  color: #999;
  margin-top: 10px;
}
.g-price {
  margin-top: 10px;
  span {
    &::before {
      content: '¥';
      font-size: 14px;
    }
    &:first-child {
      color: @priceColor;
      margin-right: 10px;
      font-size: 22px;
    }
    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}
.g-service {
  background: #f5f5f5;
  width: 500px;
  padding: 20px 10px 0 10px;
  margin-top: 10px;
  dl {
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      color: #666;
      &:last-child {
        span {
          margin-right: 10px;
          &::before {
            content: '•';
            color: @xtxColor;
            margin-right: 2px;
          }
        }
        a {
          color: @xtxColor;
        }
      }
    }
  }
}
style>

来吧试试看

你可能感兴趣的:(项目,vue.js)