vue实现省市区三级联动,借助vant-Area组件
<div>
<van-area title="标题" :area-list="areaList" @change="change" @confirm="sure" @cancel="cancel"/>
</div>
{
province_list: {
110000: '北京市',
120000: '天津市'
},
city_list: {
110100: '北京市',
110200: '县',
120100: '天津市',
120200: '县'
},
county_list: {
110101: '东城区',
110102: '西城区',
110105: '朝阳区',
110106: '丰台区'
120101: '和平区',
120102: '河东区',
120103: '河西区',
120104: '南开区',
120105: '河北区',
}
}
areaList: {
province_list: {},
city_list: {},
county_list: {}
},
provinceId: "110000",
cityId: "110100",
getData() {
let token = localStorage.getItem("token");
return province(token).then(res => {
let obj = {};
res.data.data.forEach(res => {
obj[res.id] = res.area_name;
});
this.areaList.province_list = obj;
return city(token, this.provinceId).then(res => {
let obj1 = {};
res.data.data.forEach(res => {
obj1[res.id] = res.area_name;
});
this.areaList.city_list = obj1;
return district(token, this.cityId).then(res => {
let obj3 = {};
res.data.data.forEach(res => {
obj3[res.id] = res.area_name;
});
this.areaList.county_list = obj3;
});
});
});
},
change(e, value, index) {
let token = localStorage.getItem("token");
if (index == 0) {
return city(token, value[index].code).then(res => {
let obj1 = {};
res.data.data.forEach(res => {
obj1[res.id] = res.area_name;
});
this.areaList.city_list = obj1;
return district(token, res.data.data[0].id).then(res => {
let obj3 = {};
res.data.data.forEach(res => {
obj3[res.id] = res.area_name;
});
this.areaList.county_list = obj3;
});
});
} else if (index == 1) {
return district(token, value[1].code).then(res => {
let obj3 = {};
res.data.data.forEach(res => {
obj3[res.id] = res.area_name;
});
this.areaList.county_list = obj3;
});
}
},