修复bug

This commit is contained in:
zhouyanli
2026-08-11 14:52:25 +08:00
parent 3757033744
commit 839f3ee1b7
6 changed files with 56 additions and 23 deletions

View File

@@ -45,7 +45,7 @@
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ref } from 'vue'
import {onLoad} from '@dcloudio/uni-app'
import { getVillageList,switchVillage} from '../../api/api.js'
@@ -64,15 +64,26 @@
const res = await getVillageList({ villageName })
if (res.code === 200) {
displayedCommunities.value = res.data
// 自动选中传入的小区
if (defaultVillageId.value) {
const found = displayedCommunities.value.find(
item => String(item.id) === String(defaultVillageId.value)
)
if (found) {
selectedItem.value = found
}
// 如果当前选中项不在新的列表中(搜索过滤掉了),则清空选中状态
if (selectedItem.value) {
const stillExists = displayedCommunities.value.find(
item => String(item.id) === String(selectedItem.value.id)
)
if (!stillExists) {
selectedItem.value = null
}
}
// 自动选中传入的默认小区(仅在默认小区存在于列表时)
if (defaultVillageId.value) {
const found = displayedCommunities.value.find(
item => String(item.id) === String(defaultVillageId.value)
)
if (found) {
selectedItem.value = found
}
}
} else {
uni.showToast({ title: res.msg || '获取数据失败', icon: 'none' })
}
@@ -82,11 +93,13 @@
isLoading.value = false
}
}
// 接收传入的参数
// 接收传入的参数,并初始加载小区列表
onLoad((options) => {
if (options?.villageId) {
defaultVillageId.value = options.villageId
}
// 在 onLoad 中调用初始加载,确保 defaultVillageId 已设置
fetchVillageList()
})
// 处理搜索输入(防抖)
@@ -147,11 +160,6 @@
// })
}
// 初始化
onMounted(() => {
fetchVillageList()
})
</script>
<style scoped>