Vue-native: 动态创建的 touchable-opacity 一开始都是按下的
Vue-native: Dynamically created touchable-opacity are all pressed at the beginning
当我使用 v-for 循环创建可触摸不透明度或按钮时,即使我没有触摸任何按钮,也会调用所有按钮的按下功能。但是使用与处理程序相同的功能的普通按钮没有任何问题。
<view v-for="taskDay in buttons" :key="taskDay.id" class="task-day">
<touchable-opacity
v-for="task in taskDay.next"
:key="task.id"
:on-press="handleTouch(task.id)"
class="task">
</touchable-opacity>
</view>
methods: {
handleTouch: function(id) {
console.log(id);
}
},
网上关于vue native的资料不多。有人可以帮忙吗?
我面临完全相同的问题....
更新:我更深入地研究了这个主题,结果发现这是本机库中的一个已知问题。
复选框在 React Native 中是可触摸的不透明度,按钮也是按某种顺序排列的视图组合,这就是为什么你在这两种情况下都有问题。
查看 github 中的原生基础问题:
https://github.com/GeekyAnts/NativeBase/issues/3038
不过,我可以找到解决方法。这不完全是一个复选框,我使用勾号图标来显示该项目已被选中,并且我从本机基础迭代了一个可触摸的不透明度。但是我敢肯定,通过一些样式,您可以创建一个复选框。
<scroll-view
:content-container-style="{
contentContainer: {
paddingVertical: 20
}
}"
>
<touchable-opacity
v-for="(item, index) in dataArray"
:key="index"
:onPress="
() => {
checkItem(index)
}
"
>
<text>{{ item.time }} - {{ item.name }}</text>
<image
:style="{ height: 15, width: 15, marginLeft: 15 }"
:source="require('../../../assets/icons/done.png')"
v-if="item.checked"
/>
</touchable-opacity>
</scroll-view>
在 checkItem 方法中,我使用拼接和替换项目以保持反应性:
methods: {
checkItem(index) {
this.dataArray[index].checked = true
this.dataArray.splice(index, 1, this.dataArray[index])
}
},
我的 dataArray 是这样的数组:
[{id: 1, name: 'Name', checked: true}, {id: 2, name: 'Name', checked: false}]
希望对解决您的问题有所帮助。
当我使用 v-for 循环创建可触摸不透明度或按钮时,即使我没有触摸任何按钮,也会调用所有按钮的按下功能。但是使用与处理程序相同的功能的普通按钮没有任何问题。
<view v-for="taskDay in buttons" :key="taskDay.id" class="task-day">
<touchable-opacity
v-for="task in taskDay.next"
:key="task.id"
:on-press="handleTouch(task.id)"
class="task">
</touchable-opacity>
</view>
methods: {
handleTouch: function(id) {
console.log(id);
}
},
网上关于vue native的资料不多。有人可以帮忙吗?
我面临完全相同的问题....
更新:我更深入地研究了这个主题,结果发现这是本机库中的一个已知问题。 复选框在 React Native 中是可触摸的不透明度,按钮也是按某种顺序排列的视图组合,这就是为什么你在这两种情况下都有问题。
查看 github 中的原生基础问题: https://github.com/GeekyAnts/NativeBase/issues/3038
不过,我可以找到解决方法。这不完全是一个复选框,我使用勾号图标来显示该项目已被选中,并且我从本机基础迭代了一个可触摸的不透明度。但是我敢肯定,通过一些样式,您可以创建一个复选框。
<scroll-view
:content-container-style="{
contentContainer: {
paddingVertical: 20
}
}"
>
<touchable-opacity
v-for="(item, index) in dataArray"
:key="index"
:onPress="
() => {
checkItem(index)
}
"
>
<text>{{ item.time }} - {{ item.name }}</text>
<image
:style="{ height: 15, width: 15, marginLeft: 15 }"
:source="require('../../../assets/icons/done.png')"
v-if="item.checked"
/>
</touchable-opacity>
</scroll-view>
在 checkItem 方法中,我使用拼接和替换项目以保持反应性:
methods: {
checkItem(index) {
this.dataArray[index].checked = true
this.dataArray.splice(index, 1, this.dataArray[index])
}
},
我的 dataArray 是这样的数组:
[{id: 1, name: 'Name', checked: true}, {id: 2, name: 'Name', checked: false}]
希望对解决您的问题有所帮助。