1、what:
场景:用apicloud做APP时要求长按列表中某一项对该项做出操作
2、如何做how:
- apicloud的文档里有一个监听长按的事件
api.addEventListener({
name:'longpress'
}, function(ret, err){
alert('长按了页面');
});
- vue中有个@touchstar事件,手指刚触碰到元素时触发
<li class="comment-item" v-for="(item, index) in list" :key="item.id" @touchstart="getTouchStart(index, item.id)">你的自定义列表内容</li>
- 结合1 、2两点:在手指刚触碰到列表子项时,用touchstart事件来触发getTouchStart函数,该函数中监听是否长按longpress,如果监听到是长按了,就做业务逻辑操作
//@touchstart事件调用的方法:
getTouchStart(index, _id) {
api.addEventListener({
name: 'longpress'
}, function (ret, err) {
// 长按了页面,弹出提示框
api.actionSheet({
cancelTitle: '取消',
buttons: ['删除该评论']
}, function (ret, err) {
if (ret.buttonIndex === 1) {
//弹出确认提示框
api.confirm({
title: '确认删除',
msg: '确认要删除目前' + lou + '楼评论吗?',
buttons: ['确定', '取消']
}, function (ret, err) {
if (ret.buttonIndex == 1) {
//做你的业务操作
}
});
}
});
});
}
3、为什么不用时间监听器写长按事件 ?why
我翻了一下,网上关于vue的长按事件涉及了计时器,还有touchmove,touchend,这样做太繁琐了,成本开销更大,而且丢掉了原生开发原本就有的api的优势。