vue省市区选择组件

github链接

一、下载运行后选择下图的html2canvas即可直接去到路由界面测试

在这里插入图片描述

二、组件效果图

在这里插入图片描述
在这里插入图片描述

三、源码路径(源码路径github

在这里插入图片描述

四、源码

<template>
  <div class="areaSelect-wrap">
    <el-select v-model="provinceValue" placeholder="请选择省份" @change="provinceChange">
      <el-option
        v-for="item in provinceOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <el-select v-model="cityValue" placeholder="请选择市" @change="cityChange">
      <el-option
        v-for="item in cityOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <el-select v-model="countryValue" placeholder="请选择区" @change="countryChange">
      <el-option
        v-for="item in countryOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <p></p>
<!--    <el-button @click="btnClick">提交</el-button>-->
  </div>

</template>

<script>
/**
 * areaSelect create by 油麦菜
 * createTime 2020/11/18 15:48
 */
import area from './area'
export default {
  name: 'areaSelect',
  components: {},
  filters: {},
  mixins: [],
  props: {
    provinceProp: {
      type: String,
      default: ''
    },
    cityProp: {
      type: String,
      default: ''
    },
    countryProp: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      provinceValue: '',
      cityValue: '',
      countryValue: '',
      provinceOptions: [],
      cityOptions: [],
      countryOptions: [],
      areaData: null
    }
  },
  computed: {},
  watch: {
  },
  created () {
  },
  mounted () {
    this.handleArea()
    this.init()
  },
  methods: {
    init () {
      if (this.provinceProp) {
        this.provinceValue = this.provinceProp
        this.provinceChange()
      }
      if (this.cityProp) {
        this.cityValue = this.cityProp
        this.cityChange()
      }
      if (this.countryProp) {
        this.countryValue = this.countryProp
        this.countryChange()
      }
    },
    handleArea () {
      this.provinceOptions = []
      this.provinceValue = ''
      for (const key in area['86']) {
        this.provinceOptions.push({
          value: key,
          label: area['86'][key]
        })
      }
    },
    resetCityAndCountryData () {
      this.cityValue = ''
      this.countryValue = ''
      this.cityOptions = []
      this.countryOptions = []
    },
    resetCountryData () {
      this.countryValue = ''
      this.countryOptions = []
    },
    provinceChange () {
      this.resetCityAndCountryData()
      for (const key in area[this.provinceValue]) {
        this.cityOptions.push({
          value: key,
          label: area[this.provinceValue][key]
        })
      }
      this.chooseData()
    },
    cityChange () {
      this.resetCountryData()
      for (const key in area[this.cityValue]) {
        this.countryOptions.push({
          value: key,
          label: area[this.cityValue][key]
        })
      }
      this.chooseData()
    },
    countryChange () {
      this.chooseData()
    },
    chooseData () {
      // console.log(this.provinceValue, this.cityValue, this.countryValue)
      // console.log(area['86'][this.provinceValue], area[this.provinceValue][this.cityValue], area[this.cityValue][this.countryValue])
      let data = null
      if (this.countryValue) {
        data = {
          province: {
            code: this.provinceValue,
            name: area['86'][this.provinceValue]
          },
          city: {
            code: this.cityValue,
            name: area[this.provinceValue][this.cityValue]
          },
          country: {
            code: this.countryValue,
            name: area[this.cityValue][this.countryValue]
          }
        }
      } else if (this.cityValue) {
        data = {
          province: {
            code: this.provinceValue,
            name: area['86'][this.provinceValue]
          },
          city: {
            code: this.cityValue,
            name: area[this.provinceValue][this.cityValue]
          }
        }
      } else if (this.provinceValue) {
        data = {
          province: {
            code: this.provinceValue,
            name: area['86'][this.provinceValue]
          }
        }
      } else {
        data = null
      }
      // console.log(data)
      this.areaData = data
    }
  }
}
</script>
<style lang="scss" scoped>
.areaSelect-wrap {

}
</style>

五、使用事项

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值