npm install element-china-area-data -S 或
cnpm install element-china-area-data -S
导入
以vue-cli创建的项目为例,在vue文件中的scrit标签内导入数据:
import { regionData, CodeToText } from "element-china-area-data";
使用
如下代码所示:
<template>
<div id="app">
<div>
<el-cascader
size="large"
:options="options"
v-model="selectedOptions"
@change="handleChange"
>
</el-cascader>
</div>
</div>
</template>
<script>
import { regionData, CodeToText } from "element-china-area-data";
export default {
name: "app",
data() {
return {
options: regionData,
selectedOptions: []
};
},
methods: {
handleChange() {
var loc = "";
for (let i = 0; i < this.selectedOptions.length; i++) {
loc += CodeToText[this.selectedOptions[i]];
}
alert(loc);
}
}
};
</script>
效果
如下图所示: