正在回答 回答被采纳积分+1
2回答
JakePrim
2020-04-22 17:46:02
addItemToShopcart(pendingItem) {
// 判断有没有购物车,如果没有购物车,则new 一个购物车list
// 如果有购物车,则直接把shopcartItem丢进去
var foodieShopcartCookie = this.getCookie("shopcart");
var foodieShopcart = [];
if (foodieShopcartCookie != null && foodieShopcartCookie != "" && foodieShopcartCookie != undefined) {
var foodieShopcartStr = decodeURIComponent(foodieShopcartCookie);
foodieShopcart = JSON.parse(foodieShopcartStr);
// 如果不是对象,则重新复制为空数组
if (typeof(foodieShopcart) != "object") {
foodieShopcart = [];
}
var isHavingItem = false;
// 如果添加的商品已经存在与购物车中,则购物车中已经存在的商品数量累加新增的
for(var i = 0 ; i < foodieShopcart.length ; i ++) {
var tmpItem = foodieShopcart[i];
var specId = tmpItem.specId;
if (specId == pendingItem.specId) {
isHavingItem = true;
var newCounts = tmpItem.buyCounts + pendingItem.buyCounts;
tmpItem.buyCounts = newCounts;
// 删除主图在数组中的位置
foodieShopcart.splice(i, 1, tmpItem);
}
}
if (!isHavingItem) {
foodieShopcart.push(pendingItem);
}
} else {
foodieShopcart.push(pendingItem);
}
console.log("addshopcart cookie",JSON.stringify(foodieShopcart));
this.setCookie("shopcart", JSON.stringify(foodieShopcart));
console.log("getCookie"+this.getCookie("shopcart"));
},在发布的前端项目 打印结果是:
setCookie 之后在getCookie 获取的为null 不知道为啥


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星