Vue component development: City selector component implementation method
In Vue development, the city selector component is a very common and practical component. It is usually used for interactive operations when users select a city or region. This article will introduce a Vue-based city selector component implementation method and provide specific code examples.
<template> <div class="city-selector"> <ProvinceSelector @provinceChange="handleProvinceChange" /> <CitySelector :province="selectedProvince" @cityChange="handleCityChange" /> <AreaSelector :city="selectedCity" @areaChange="handleAreaChange" /> <div class="selected-result"> <p>已选城市:{{ selectedCity }}</p> <p>已选区域:{{ selectedArea }}</p> </div> </div> </template> <script> import ProvinceSelector from './ProvinceSelector.vue' import CitySelector from './CitySelector.vue' import AreaSelector from './AreaSelector.vue' export default { components: { ProvinceSelector, CitySelector, AreaSelector }, data() { return { selectedProvince: '', selectedCity: '', selectedArea: '' } }, methods: { handleProvinceChange(province) { this.selectedProvince = province this.selectedCity = '' this.selectedArea = '' }, handleCityChange(city) { this.selectedCity = city this.selectedArea = '' }, handleAreaChange(area) { this.selectedArea = area } } } </script>
ProvinceSelector.vue:
<template> <div class="province-selector"> <select v-model="selectedProvince" @change="handleChange"> <option v-for="province in provinces" :key="province" :value="province">{{ province }}</option> </select> </div> </template> <script> export default { data() { return { selectedProvince: '' } }, computed: { provinces() { // 从后台或静态数据获取省份列表 return ['北京', '上海', '广东', '江苏'] } }, methods: { handleChange() { this.$emit('provinceChange', this.selectedProvince) } } } </script>
CitySelector.vue:
<template> <div class="city-selector"> <select v-model="selectedCity" @change="handleChange"> <option v-for="city in cities" :key="city" :value="city">{{ city }}</option> </select> </div> </template> <script> export default { props: { province: { type: String, default: '' } }, data() { return { selectedCity: '' } }, computed: { cities() { // 根据选择的省份从后台或静态数据获取城市列表 if (this.province === '北京') { return ['北京市', '朝阳区', '海淀区'] } else if (this.province === '上海') { return ['上海市', '黄浦区', '徐汇区'] } else if (this.province === '广东') { return ['广州市', '深圳市', '珠海市'] } else if (this.province === '江苏') { return ['南京市', '苏州市', '无锡市'] } else { return [] } } }, watch: { province(newValue) { this.selectedCity = '' } }, methods: { handleChange() { this.$emit('cityChange', this.selectedCity) } } } </script>
AreaSelector.vue:
<template> <div class="area-selector"> <select v-model="selectedArea" @change="handleChange"> <option v-for="area in areas" :key="area" :value="area">{{ area }}</option> </select> </div> </template> <script> export default { props: { city: { type: String, default: '' } }, data() { return { selectedArea: '' } }, computed: { areas() { // 根据选择的城市从后台或静态数据获取区域列表 if (this.city === '北京市') { return ['东城区', '西城区', '朝阳区'] } else if (this.city === '上海市') { return ['黄浦区', '徐汇区', '长宁区'] } else if (this.city === '广州市') { return ['越秀区', '海珠区', '天河区'] } else if (this.city === '南京市') { return ['玄武区', '秦淮区', '鼓楼区'] } else { return [] } } }, watch: { city(newValue) { this.selectedArea = '' } }, methods: { handleChange() { this.$emit('areaChange', this.selectedArea) } } } </script>
<template> <div> <CitySelector /> </div> </template> <script> import CitySelector from './CitySelector.vue' export default { components: { CitySelector } } </script>
The above is the implementation method of a city selector component based on Vue. By splitting and implementing the functions of each sub-component, we can flexibly combine and call these sub-components to implement a complete city selector. I hope this article will help you understand and learn Vue component development.
The above is the detailed content of Vue component development: City selector component implementation method. For more information, please follow other related articles on the PHP Chinese website!