• 封装一个省市区公共组件


    难点:封装一个省市区三级联动的公共组件,难点在与对三级嵌套数据的处理,其中三级嵌套数据来源于github上的Administrative-divisions-of-china

    <template>
    	<el-select v-model="province" class="m-2" placeholder="请选择省份" size="small">
    	  <el-option
    		v-for="item in areas"
    		:key="item.code"
    		:label="item.name"
    		:value="item.code"
    	  />
    	</el-select>
    	<el-select v-model="city" :disabled="!province" class="m-2" placeholder="请选择市" size="small">
    	  <el-option
    		v-for="item in selectCitys"
    		:key="item.code"
    		:label="item.name"
    		:value="item.code"
    	  />
    	</el-select>
    	<el-select v-model="area" :disabled="!city" class="m-2" placeholder="请选择区" size="small">
    	  <el-option
    		v-for="item in selectAreas"
    		:key="item.code"
    		:label="item.name"
    		:value="item.code"
    	  />
    	</el-select>
      </template>
      
      <script setup>
      import { ref, watch, defineEmits } from 'vue'
      import allAreas from './lib/pca-code.json'
      
      const areas = ref(allAreas)
      const selectCitys = ref([])
      const selectAreas = ref([])
      const province = ref('')
      const city = ref('')
      const area = ref('')
      const emit = defineEmits(['change'])
    
      watch(province,async (value) => {
    	if(value) {
    		let citys = areas.value.find(item => 
    			item.code===province.value
    		)?.children
    		selectCitys.value = citys
    		city.value = ''
    		area.value = ''
    	}else{
    		return
    	}
      })
      
      watch(city,async (value) => {
    	if(value) {
    		let areas = selectCitys.value.find(item => 
    			item.code===city.value
    		)?.children
    		selectAreas.value = areas
    		area.value = ''
    	}else{
    		return
    	}
      })
    
      watch(area,async (value) => {
    	if(value) {
    		let provinceData = {
    			code: province.value,
    			name: areas.value.find(item => item.code===province.value)?.name,
    		}
    		let cityData = {
    			code: city.value,
    			name: selectCitys.value.find(item => item.code===city.value)?.name,
    		}
    		let areaData = {
    			code: area.value,
    			name: selectAreas.value.find(item => item.code===area.value)?.name
    		}
    		emit('change',{
    			provinceData,
    			cityData,
    			areaData
    		})
    	}
      })
    
    
      </script>
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
  • 相关阅读:
    尚硅谷webpack5笔记2
    C#面向对象程序设计课程实验二: 实验名称:Windows 窗体程序
    CC26XX睡眠
    【节能学院】智能操控装置在高压开关柜的应用
    亚马逊测评关于IP和DNS的问题
    WebSocket、event-source、AJAX轮询 等实现保持前后端实时通信的方式
    干货,深入剖析ReentrantLock源码,推荐收藏
    计算机二级考试题库及答案
    JavaScript知识点复习--(二)函数高级
    XML快速入门
  • 原文地址:https://blog.csdn.net/weixin_47204963/article/details/134252441