我的项目中用到了微信提供的第三方自定义组件slide-view,而且是循环使用,
具体使用如下:
wxml文件:
{{item.name}}
删除
json文件:
{
"component": true,
"usingComponents": {
"slide-view": "/my/component/base/wechatComponent/slideView/slideView"
}
}
问题如下,删除标签6:

删除后

从上图中发现,我删除标签6后,因为是将tagArray里标签6的信息给删除,会出现如图所示的问题,现在我想在deleteTagFunc里增加一个回弹的方法,在删除一项时先将该slide-view的偏移量置零再删除,已避免上述问题的发生,
于是在js文件里增加
deleteTagFunc: function(e) {
let self = this;
wx.showModal({
title: '提示',
content: '确认删除标签',
confirmText: '是',
cancelText: '否',
success(res) {
if (res.confirm) {
self.deleteTag(e.currentTarget.id);
}
}
})
},
deleteTag: function (tagId) {
console.log(tagId);
//回弹
let slideView = this.selectComponent('#' + tagId);
console.log(slideView);
slideView.moveBack(); //slide-view内部增加方法 偏移量置零
//其他删除逻辑
。。。
}
发现输出tagId有值,slideView 有时有值,有时为空,导致moveBack方法无法调用而报错,请问为什么selectComponent获取不到自定义组件
ps:我尝试过将delteTag里的tagId写死,同样获取不到
微信小程序开发问题解答
微信小程序开发者回答:
wx:key 不能用index,你给每一项给个独一无二的id
本文网址:http://www.91bianli.com/weixinxiaochengxu/19195.html