VUE3:省市区联级选择器
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
免费下载资源
·
一、实现效果
二、代码展示
<template>
<div class="page">
<select v-model="property.province">
<option v-for="item in provinces" :key="item">
{{ item }}
</option>
</select>
<select v-model="property.city">
<option v-for="item in cities" :key="item">
{{ item }}
</option>
</select>
<select v-model="property.district">
<option v-for="item in districts" :key="item">{{ item }}</option>
</select>
</div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from "vue";
interface TreeNode {
name: string;
children?: TreeNode[];
}
const property = reactive({
// 省/直辖市/自治区/特别行政区
province: "",
//市
city: "",
//区
district: "",
});
// 数据
const tree = ref({
name: "中国",
children: [
{
name: "广东省",
children: [
{
name: "广州市",
children: [
{
name: "天河区",
},
{
name: "越秀区",
},
],
},
{
name: "深圳市",
children: [
{
name: "福田区",
},
{
name: "南山区",
},
],
},
],
},
{
name: "四川省",
children: [
{
name: "成都市",
children: [
{
name: "锦江区",
},
{
name: "武侯区",
},
],
},
{
name: "绵阳市",
children: [
{
name: "涪城区",
},
{
name: "游仙区",
},
],
},
],
},
{
name: "北京市",
children: [
{
name: "东城区",
},
{
name: "西城区",
},
],
},
],
});
// 所有省/直辖市/自治区/特别行政区
const provinces = tree.value.children.map((item) => item.name);
// 根据省/直辖市/自治区/特别行政区获取市
const cities = computed(() => {
const province = tree.value.children.find(
(item) => item.name === property.province
);
return province?.children?.map((item) => item.name) || [];
});
// 根据市获取区
const districts = computed(() => {
const province = tree.value.children.find(
(item) => item.name === property.province
);
const city: TreeNode | undefined = province?.children?.find(
(item) => item.name === property.city
);
return city?.children?.map((item) => item.name) || [];
});
</script>
<style scoped lang="scss">
.page {
width: 100%;
height: 100vh;
background-color: rgb(7, 14, 17);
color: #ffffff;
display: flex;
justify-content: center;
align-items: center;
select {
width: 100px;
height: 30px;
}
}
</style>
再找AI要个完整的数据就行
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 4 个月前
e428d891
Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
更多推荐
已为社区贡献10条内容
所有评论(0)