基于vue+element的省市区(默认二级)三级下拉选择框组件

组件代码:组件命名随意,暂定regional-selection

<template>
  <div>
    <el-cascader
    :value="value"
    :options="options"
    :placeholder="placeholder"
    style="width:100%"
    size="small"
    clearable
    @change="handleChange">
    </el-cascader>
  </div>
</template>

<script>
// import adress from './adress.json'
// import secondAdress from './secondAdress.json'
// import address from './response.json'
import { getLabelList } from './api.js'
export default {
  /*
      type:two =>2级 ;three =>3级
  */
  name: 'areaSelect',
  props: {
    type: { type:String, default:'two' },
    placeholder: { type:String, default:'请选择' },
    value: { type:Array }
  },
  data () {
      return {
      options:[],
    }
  },
  async created () {
    let res= await getLabelList()
    console.log(res)
    let address = res.data.data
    if(this.type === 'two'){
      address.forEach(item=>{        
        if(item.hasChildren){
          item.children.forEach(index=>{
              delete index.children
          })
        }
        this.options.push(item)
      })
    }
    else {
      address.forEach(item=>{        
        if(item.hasChildren){
          item.children.forEach(index=>{
              if(index.hasChildren){
                index.children.forEach(chil=>{
                  delete chil.children
                })
              }
          })
        }
        this.options.push(item)
      })
      // this.options = address
    }
},
  methods: {
    handleChange(value){
      // console.log(value)
      this.$emit('change',value)
      this.$emit('input',value)
    }
  }
}
</script>

<style lang='scss' scoped>
</style>

main.js中全局注册:全局组件areaSelect

// 地区二级 三级联动选择
import areaSelect from './components/regional-selection'
Vue.component('areaSelect',areaSelect)

页面中使用

<div v-if="fenceTypeShow">
    <el-form-item label="行政区域:" prop="areaList">
        <area-select
            v-model="form.areaList"
            placeholder=""
            style="width: 240px;"
            type ='three'
            @change="fenceSelChange"
        />
    </el-form-item>
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的小辣条

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值