以下教程参考自官方的教程:https://www.npmjs.com/package/element-china-area-data
安装
- 执行以下语句安装城市数据:
npm 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>