2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
In the original js thinking, the selection of cascading drop-down boxes is often to first bind the menu of the first-level drop-down box, then onchange in the drop-down box, get the current option in the onchange event, and then bind the data of the second-level drop-down box, and so on...
In the Vue framework, you should change your thinking. First, set the data of the first-level drop-down box, then watch the option. If it changes, set the data of the second-level drop-down box, and so on:
- <el-form-item label="省">
- <el-select v-model="where.provinceId" placeholder="请选择省份" clearable >
- <el-option v-for="item in provinces" :key="item.id" :label="item.name" :value="item.id" />
- </el-select>
- </el-form-item>
- <el-form-item label="市">
- <el-select v-model="where.cityId" placeholder="请选择市" clearable>
- <el-option v-for="item in cities" :key="item.id" :label="item.name" :value="item.id" />
- </el-select>
- </el-form-item>
- const provinces = ref([])
- const cities = ref([])
-
- onMounted(()=>{
- // 通过接口获取省份
- provinces.value = [...data]
- })
-
- watch: {
- provinceId: {
- deep: true,
- handler() {
- // 根据接口获取市的数据
- cities.value=[...data]
- // 清空城市的选择
- where.cityId=''
- }
- }
- },