1、安装组件依赖
- npm install element-china-area-data -S
2、目标页面中引入
- import {
provinceAndCityData,
regionData,
provinceAndCityDataPlus,
regionDataPlus,
CodeToText,
TextToCode
} from "element-china-area-data";
3、使用el-cascader标签
- v-model="selectedOptions" @change="addressChoose">
4、data中定义元素
- data(){
return{
// 省市区级联
options: regionDataPlus,
selectedOptions: [],
5、触发事件的函数方法
- /**省市区三级联动 */
addressChoose(value) {
console.log(
"省市区:",
CodeToText[value[0]],
CodeToText[value[1]],
CodeToText[value[2]]
this.form.province = CodeToText[value[0]];
this.form.city = CodeToText[value[1]];
this.form.area = CodeToText[value[2]];
},
6、数据回显
- // 省市区转换
let province = this.form.province;
let city = this.form.city;
let area = this.form.area;
let arg1 = TextToCode[province].code;
let arg2 = TextToCode[province][city].code;
let arg3 = TextToCode[province][city][area].code;
this.selectedOptions.push(arg1, arg2, arg3);
按照步骤,直接C+V复制到项目中,就可以运行实现图中效果。也可以从开源字节的代码仓库fork租房源码。
如若转载,请注明出处:开源字节 https://sourcebyte.cn/article/267.html
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.