Home  >  Article  >  Web Front-end  >  Vue component development: City selector component implementation method

Vue component development: City selector component implementation method

王林
王林Original
2023-11-24 08:29:051698browse

Vue component development: City selector component implementation method

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.

  1. Requirements Analysis
    Before starting to write code, we first need to clarify the requirements of the component. The city selector needs to have the following functions:
  2. Display an optional list of provinces
  3. According to the selected province, dynamically display the corresponding city list
  4. According to the selected city, dynamically display Display the corresponding region list
  5. Get the city and region finally selected by the user
  6. Component structure design
    According to needs, we can split the city selector component into three sub-components: ProvinceSelector ( Province Selector), CitySelector (City Selector) and AreaSelector (Area Selector). Among them, the ProvinceSelector and CitySelector components can communicate by passing props and events. The structure of the entire component is as follows:
<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>
  1. Sub-component implementation
    Next, we write the province selector, city selector and region selector respectively.

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>
  1. Using components
    After completing the writing of the component, just introduce it where you need to use the city selector:
<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn