vue通过json文件实现省市县三级联动
时间: 2023-07-31 21:12:58 浏览: 113
可以通过以下步骤实现vue通过json文件实现省市县三级联动:
1. 创建一个json文件,里面包含了省市县的数据,格式可以参考以下示例:
```json
{
"provinces": [
{
"name": "北京市",
"cities": [
{
"name": "北京市",
"counties": [
{
"name": "东城区"
},
{
"name": "西城区"
},
...
]
}
]
},
{
"name": "天津市",
"cities": [
{
"name": "天津市",
"counties": [
{
"name": "和平区"
},
{
"name": "河东区"
},
...
]
}
]
},
...
]
}
```
2. 在vue组件中引入该json文件,并将数据存储在data中:
```javascript
<template>
<div>
<select v-model="province" @change="handleProvinceChange">
<option value="">请选择省份</option>
<option v-for="province in provinces" :value="province">{{ province.name }}</option>
</select>
<select v-model="city" @change="handleCityChange">
<option value="">请选择城市</option>
<option v-for="city in cities" :value="city">{{ city.name }}</option>
</select>
<select v-model="county">
<option value="">请选择县区</option>
<option v-for="county in counties" :value="county">{{ county.name }}</option>
</select>
</div>
</template>
<script>
import data from './data.json'
export default {
data() {
return {
provinces: data.provinces,
cities: [],
counties: [],
province: '',
city: '',
county: ''
}
},
methods: {
handleProvinceChange() {
this.cities = this.province.cities
this.counties = []
this.city = ''
this.county = ''
},
handleCityChange() {
this.counties = this.city.counties
this.county = ''
}
}
}
</script>
```
3. 在模板中使用三个select标签来展示省市县选择框,并绑定对应的数据和事件处理函数。
这样就可以通过json文件实现省市县三级联动了。
阅读全文