<block wx:for="{{ziliaomuluhechengdata}}" wx:key="index" wx:for-item="item" style="display: flex; justify-content: space-between; height: 20px;">
<!-- 图片逻辑 -->
<view style="width: 100%; display: flex; align-items: center;">
<view wx:if="{{item.wenjianleixing=='image'}}" style="width: 100%;">
<view style="display: flex; align-items: center; ">
<view class="tag" bindtap="toggleImageVisibility" data-index="{{index}}" style="width:{{windowWidth-60}}px;">
{{item.wenjianbiaoti}}︾
</view>
<view style="float: left;">
<image wx:if="{{item.shoucang==false}}" src="{{gray_star}}" bindtap="toggleStar" data-index="{{index}}" style="width: 20px; height: 20px; margin-right: 5px;">收藏</image>
<image wx:else="{{item.shoucang==true}}" src="{{green_star}}" bindtap="toggleStar" data-index="{{index}}" style="width: 20px; height: 20px; margin-right: 5px;">收藏</image>
</view>
</view>
<view wx:if="{{showImage[index]}}" style="width: 100%;">
<view wx:for="{{item.wenjianurl}}" wx:key="uniqueKey" wx:for-item="itemone" style="width: 100%;">
<image src="{{itemone}}" bindload="imageLoaded" data-index="{{index}}" mode="widthfix" style="width: {{windowWidth-20}}px; height: {{imageHeights[index]}}px;"></image>
</view>
</view>
</view>
<view wx:else="{{item.wenjianleixing=='pdf'}}">
<view style="display: flex; align-items: center;">
<view class="tag" bindtap="openPdf" data-index="{{index}}" style="width:{{windowWidth-60}}px;">
{{item.wenjianbiaoti}}︾
</view>
<view style="float: left;">
<image wx:if="{{item.shoucang==false}}" src="{{gray_star}}" bindtap="toggleStar" data-index="{{index}}" style="width: 20px; height: 20px; margin-right: 5px;">收藏</image>
<image wx:else="{{item.shoucang==true}}" src="{{green_star}}" bindtap="toggleStar" data-index="{{index}}" style="width: 20px; height: 20px; margin-right: 5px;">收藏</image>
</view>
</view>
</view>
</view>
</block>
toggleStar: function(e) {
console.log('传入函数的e',e);
let index = e.currentTarget.dataset.index;
console.log('当前点击的标签',index)
let item = this.data.ziliaomuluhechengdata[index];
console.log('对应的 ziliaomuluhechengdata[index]', item);
console.log('当前传入的收藏标签',item.shoucang)
if (item.shoucang==false) {
item.shoucang = !item.shoucang; // 直接修改 item 对象中的 shoucang 值为 true
this.setData({
['ziliaomuluhechengdata[' + index + '].shoucang']: item.shoucang
});
console.log('取反收藏标记的数据', this.data.ziliaomuluhechengdata);
app.globalData.ziliaoshoucangjia.push(item);
console.log('全局收藏夹数据', app.globalData.ziliaoshoucangjia);
}
if (item.shoucang==true) {
// 先找到要删除的 item 在全局数组中的索引
const removeIndex = app.globalData.ziliaoshoucangjia.findIndex((value) => value === item);
if (removeIndex!== -1) { app.globalData.ziliaoshoucangjia.splice(removeIndex, 1); }
}
console.log('删除当前收藏后的item数组',app.globalData.ziliaoshoucangjia)
item.shoucang = false; // 直接修改 item 对象中的 shoucang 值为 false
this.setData({
['ziliaomuluhechengdata[' + index + '].shoucang']: item.shoucang
});
console.log('取消收藏标记后的数组',this.data.ziliaomuluhechengdata)
}
这段代码,数组里初始的item.shoucang的值都为false。无论我把代码改成两个if。还是if else 还是if else if。它都同时执行了两个逻辑分支,似乎程序在这里,无论怎么修改判断,无论怎么修改语句都像是同时具备了false和true两个矛盾的属性