//对象的方式————数据是无序的
addToCart(){
// 先取本地存储,假设本地有,先取,因为只有取到了才可以做修改
//一定要写默认值
let cart=uni.getStorageSync('cart') || {}
// 更新存储的数据
let goodsId=this.storeDetail.goods_id
if(cart[goodsId]){
cart[goodsId]={
num:cart[goodsId].num+1,
checked:true
};
}else{
cart[goodsId]={
num:1,
checked:true
};
}
//将更新之后的信息存储起来
uni.setStorageSync('cart',cart)
},
//对以上方法的优化——————对象的方式,数据是无序的
// 先取本地存储,假设本地有,先取,因为只有取到了才可以做修改
let cart=uni.getStorageSync('cart') || {}
// 更新存储的数据
let goodsId=this.storeDetail.goods_id
cart[goodsId]={
num:cart[goodsId]?cart[goodsId].num+1:1,
checked:true
}
//将更新之后的信息存储起来
uni.setStorageSync('cart',cart)
}