fix: 修复城市选择列表点击城市信息无法获取参数bug
This commit is contained in:
parent
ffe6f6239b
commit
424c1e8df7
23
README-zh.md
23
README-zh.md
@ -37,3 +37,26 @@ Git 提交规范
|
|||||||
- list 列表区域
|
- list 列表区域
|
||||||
|
|
||||||
|
|
||||||
|
循环调用避坑指南:
|
||||||
|
- 页面: page/shopcity/index.vue 城市选择列表页
|
||||||
|
- 背景: 城市名称列表依赖于父级城市英文首字母简写例:A、B、C, 循环项下的list 进行循环展示
|
||||||
|
- 场景: 城市列表点击,需要获取点击的城市信息
|
||||||
|
- 问题: 触发点击事件函数无法接收入参item
|
||||||
|
- 解决方案: 改为箭头函数 @click="() => { headerSelectMapClick(item) }"
|
||||||
|
- 原因: 查询DeepSeek给出的解释是, 箭头函数创建了一个闭包,保留了 item 的引用 即使列表重新渲染,也能保持对正确 item 的引用
|
||||||
|
- 该解释是否正确 不确定 有待考量
|
||||||
|
- 示例代码
|
||||||
|
<!--
|
||||||
|
<scroll-view class="list-scroll" scroll-y :scroll-into-view="activeId" @scroll="handleScroll">
|
||||||
|
<view v-for="(group, index) in groupedData" :key="index" class="white_container" :id="'group-' + group.letter">
|
||||||
|
<view class="letter-title">{{ group.letter }}</view>
|
||||||
|
<view v-for="(item, ind) in group.list"
|
||||||
|
:key="item.id"
|
||||||
|
:class="['list-item', ind === group.list.length - 1 && 'no_border']"
|
||||||
|
@click="headerSelectMapClick(item)" // TODO:问题点 为什么无法获取点击的item参数
|
||||||
|
>
|
||||||
|
{{ item.name }}
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
|
</scroll-view>
|
||||||
|
-->
|
||||||
@ -67,11 +67,12 @@
|
|||||||
<!-- 字母标题 -->
|
<!-- 字母标题 -->
|
||||||
<view class="letter-title">{{ group.letter }}</view>
|
<view class="letter-title">{{ group.letter }}</view>
|
||||||
<!-- 列表项 -->
|
<!-- 列表项 -->
|
||||||
|
<!-- TODO: 小程序编译避坑指南: -->
|
||||||
<view
|
<view
|
||||||
v-for="(item, ind) in group.list"
|
v-for="(item, ind) in group.list"
|
||||||
:key="item.id"
|
:key="item.id"
|
||||||
:class="['list-item', ind === group.list.length - 1 && 'no_border']"
|
:class="['list-item', ind === group.list.length - 1 && 'no_border']"
|
||||||
@click="headerSelectMapClick(item)"
|
@click="() => { headerSelectMapClick(item) }"
|
||||||
>
|
>
|
||||||
{{ item.name }}
|
{{ item.name }}
|
||||||
</view>
|
</view>
|
||||||
@ -164,9 +165,10 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
headerSelectMapClick(item) {
|
headerSelectMapClick(item) {
|
||||||
|
console.log('1111 this', this);
|
||||||
console.log("地图选点", item);
|
console.log("地图选点", item);
|
||||||
const lat = Number(item.lat);
|
const lat = Number(item.lat) || 0;
|
||||||
const long = Number(item.long);
|
const long = Number(item.long) || 0;
|
||||||
uni.chooseLocation({
|
uni.chooseLocation({
|
||||||
latitude: lat,
|
latitude: lat,
|
||||||
longitude: long,
|
longitude: long,
|
||||||
@ -222,6 +224,7 @@ export default {
|
|||||||
}, 500),
|
}, 500),
|
||||||
|
|
||||||
groupData(item) {
|
groupData(item) {
|
||||||
|
console.log('123. item', item);
|
||||||
const map = {};
|
const map = {};
|
||||||
this.letters.forEach((letter) => {
|
this.letters.forEach((letter) => {
|
||||||
map[letter] = {
|
map[letter] = {
|
||||||
@ -235,6 +238,7 @@ export default {
|
|||||||
map[items.letters].list.push(items);
|
map[items.letters].list.push(items);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log('11111111', map)
|
||||||
|
|
||||||
// 过滤掉没有数据的字母
|
// 过滤掉没有数据的字母
|
||||||
this.groupedData = Object.values(map).filter(
|
this.groupedData = Object.values(map).filter(
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -3,5 +3,18 @@
|
|||||||
"projectname": "uniapp-ZHSQ",
|
"projectname": "uniapp-ZHSQ",
|
||||||
"setting": {
|
"setting": {
|
||||||
"compileHotReLoad": true
|
"compileHotReLoad": true
|
||||||
|
},
|
||||||
|
"condition": {
|
||||||
|
"miniprogram": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"name": "pages/shopcity/shopcity",
|
||||||
|
"pathName": "pages/shopcity/shopcity",
|
||||||
|
"query": "",
|
||||||
|
"scene": null,
|
||||||
|
"launchMode": "default"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user