npm install @luohc92/vue3-area-selector
import {
AreaSelector,
} from "@luohc92/vue3-area-selector";
import "@luohc92/vue3-area-selector/dist/style.css";
在main.js中引入
import AreaSelector from "@luohc92/vue3-area-selector";
import "@luohc92/vue3-area-selector/dist/style.css";
const app = createApp(App);
app.use(AreaSelector);
<template>
<div class="app">
<AreaSelector
@change="change"
@open="open"
@close="close"
:props="areaSelectProps"
v-model="area"
>
</AreaSelector>
</div>
</template>
<script setup lang="ts">
import { reactive, ref } from "vue";
import {
AreaSelector,
AreaDataDto,
AreaSelectorPropsDto,
AreaSelectorResultDto,
Resolve,
} from "@luohc92/vue3-area-selector";
import "@luohc92/vue3-area-selector/dist/style.css";
const areaSelectProps = reactive<AreaSelectorPropsDto>({
popperClass: "my-area-popup",
townLazy: true,
level: 0,
showAllLevels: true,
disabled: [],
lazyLoad: (node: AreaDataDto, resolve: Resolve) => {
return setTimeout(() => {
if (node.code === "110101") {
const data: AreaDataDto[] = [{ code: "110101001", name: "东华门街道" }];
resolve(data);
} else if (node.code === "110102") {
const data: AreaDataDto[] = [{ code: "110101002", name: "西华门街道" }];
resolve(data);
} else {
resolve();
}
}, 2000);
},
});
const area = ref<AreaSelectorResultDto>({
town: "东华门街道",
district: "东城区",
code: "110101001",
city: "市辖区",
province: "北京市",
});
const change = (data: AreaSelectorResultDto) => {
console.log(data);
};
const open = () => {
console.log("open");
};
const close = (data: AreaSelectorResultDto) => {
console.log("close", data);
};
</script>
--area-selector-panel-bg: #ffffff;
--area-selector-input-bg: #ffffff;
--area-selector-border-color: #dcdfe6;
--area-selector-disable-bg: #f5f7fa;
--area-selector-placeholder-color: #a8abb2;
--area-selector-active-color: #409eff;
--area-selector-text-color: #606266;
--area-selector-list-item-hover-color: #f5f5f5;
--area-selector-list-item-disabled-color: #aaaaaa;
--area-selector-clearable-color: #aaaaaa;