收藏
1,页面(iscollect是用来判断是否被收藏)
2,在index.js中对bindtap写事件
data:{
//iscollect初始值
iscollect:false
}
//事件
shoucang(){
//1,从本地存储里先创建shoucang
let shoucang=wx.getStorageSync('shoucang')||[];
/,2,点击的数据通过findIndex进行查询shoucang存储是否有此数据
//this.data.all.goods_id是此页面的数据
let index=shoucang.findIndex(v=>v.goods_id===this.data.all.goods_id)
if(index===-1){
//3,如果index返回-1证明没有找到,此时添加
shoucang.push(this.data.all)
wx.setStorageSync('shoucang',shoucang)
wx.showToast({
title: '添加成功',
icon:"success",
mask:true,
})
//将收藏的状态更改
this.setData({
isCollect:true
})
}else{
//4,如果返回0就是代表数据里存