更新记录
2.0.0(2025-07-09) 下载此版本
发布vue3版本,vue2版本不变,示例工程中 为vue3版用法
1.0.0(2025-07-05) 下载此版本
版本发布
平台兼容性
uni-app(4.06)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - | - | - |
vshare-address-picker
更新:已支持 Vue3!
- Vue2 版本组件文件:
vshare-address-picker.vue
- Vue3 版本组件文件:
vshare-address-picker2.vue
基于uniapp的无限级地址选择器,支持单选/多选、异步加载、搜索功能,适用于省市区/自定义多级分类等场景。
功能特性
- 无限级联动选择,层级不限
- 单选/多选模式切换
- 支持异步加载每一级数据
- 支持本地/远程数据源
- 支持搜索功能
- 字段名可自定义
- 事件丰富,易于集成
- 适配uniapp多端
安装与引入
Vue2 用法
// 方式一:页面中直接引入
import VshareAddressPicker from '@/components/vshare-address-picker/vshare-address-picker.vue'
export default {
components: { VshareAddressPicker }
}
// 方式二:全局注册
import VshareAddressPicker from '@/components/vshare-address-picker'
Vue.use(VshareAddressPicker)
Vue3 用法
// 方式一:页面中直接引入
import VshareAddressPicker2 from '@/components/vshare-address-picker/vshare-address-picker2.vue'
export default {
components: { VshareAddressPicker2 }
}
基本用法
Vue2
<template>
<vshare-address-picker
:data="addressData"
:multiple="false"
:searchable="true"
:asyncLoad="loadChildren"
:value="selectedPath"
@change="onChange"
@confirm="onConfirm"
@cancel="onCancel"
@search="onSearch"
/>
</template>
<script>
export default {
data() {
return {
addressData: [], // 第一级数据
selectedPath: []
}
},
methods: {
// 异步加载子节点
loadChildren(parent, level) {
// 返回Promise<children[]>
return fetch(`/api/children?parentId=${parent.value}`)
.then(res => res.json())
},
onChange(val) { console.log('change', val) },
onConfirm(val) { console.log('confirm', val) },
onCancel() { console.log('cancel') },
onSearch(keyword) { console.log('search', keyword) }
}
}
</script>
Vue3
<template>
<vshare-address-picker2
:data="addressData"
:multiple="false"
:searchable="true"
:asyncLoad="loadChildren"
:value="selectedPath"
@change="onChange"
@confirm="onConfirm"
@cancel="onCancel"
@search="onSearch"
/>
</template>
<script setup>
import { ref } from 'vue'
import VshareAddressPicker2 from '@/components/vshare-address-picker/vshare-address-picker2.vue'
defineProps({})
const addressData = ref([])
const selectedPath = ref([])
function loadChildren(parent, level) {
// 返回Promise<children[]>
return fetch(`/api/children?parentId=${parent.value}`)
.then(res => res.json())
}
function onChange(val) { console.log('change', val) }
function onConfirm(val) { console.log('confirm', val) }
function onCancel() { console.log('cancel') }
function onSearch(keyword) { console.log('search', keyword) }
</script>
Props
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
data | Array | [] | 第一级数据 |
asyncLoad | Function | null | 异步加载函数(parent, level) => Promise<children[]> |
value | Array | [] | 当前选中路径(单选/多选) |
multiple | Boolean | false | 是否多选 |
searchable | Boolean | false | 是否可搜索 |
searchPlaceholder | String | '搜索地址' | 搜索框占位符 |
labelKey | String | 'label' | 显示字段名 |
valueKey | String | 'value' | 值字段名 |
childrenKey | String | 'children' | 子节点字段名 |
filterMethod | Function | null | 自定义搜索过滤方法 |
maxLevel | Number | 0 | 最大选择层级,0为不限 |
事件
事件名 | 说明 | 回调参数 |
---|---|---|
change | 选中项变化时触发 | 当前选中路径(数组) |
confirm | 点击确定时触发 | 当前选中路径(数组) |
cancel | 点击取消时触发 | 无 |
search | 搜索时触发 | 关键词 |
方法(通过ref)
- 暂无(可根据需求扩展)
多选说明
multiple=true
时,最后一级可多选,返回所有选中项的路径数组。
样式自定义
- 可通过scoped样式或覆盖类名自定义外观。
示例
详见 example.vue
文件。
MIT License