这个文件在page 同名的js下面(https://opendocs.alipay.com/mini/framework/page-detail)
```java
//变量定义
var dish_all_cost = 0;
var dish_all_menu = "";
var current_bottom_menu_show = false;
//
Page({
//页面的数据
data: {
dish_count: 0,
dish_menu: "当前购物车是空的",
bottom_menu_show: false,
done_dishes: [
{
id: 1,
name: "可乐",
},
{
id: 2,
name: "薯条",
},
]
,
},
//这里定义的事件
handlebottomView(e) {
console.log("底部菜单被点击了");
if (!current_bottom_menu_show) {
this.setData({ bottom_menu_show: true });
} else {
this.setData({ bottom_menu_show: false });
}
current_bottom_menu_show = !current_bottom_menu_show;
},
handleDish(e) {
// 获取到当前事件
const dishInfo = e.target.dataset.dishInfo || {};
console.info("CLICK" + dishInfo.name);
dish_all_cost = dish_all_cost + dishInfo.price;
dish_all_menu = dish_all_menu + dishInfo.name + ";";
this.setData({ dish_menu: dish_all_menu });
this.setData({ dish_count: dish_all_cost });
var valueAble = false;
console.log("dishInfo"+dishInfo.id);
//https://www.cnblogs.com/m2maomao/p/7743143.html
//这里的循环为什么要of
//总结来说:for in总是得到对像的key或数组,字符串的下标,而for of和forEach一样,是直接得到值
for (var item of this.data.dishes) {
console.log("item"+item.id);
if( item.id == dishInfo.id){
valueAble = true;
break;
} else {
continue;
}
}
if(!valueAble){
console.log("当前点击不合法");
return;
} else {
console.log("当前点击合法");
}
for (var itemDone of this.data.done_dishes){
if(itemDone.id == dishInfo.id) {
var count = itemDone.amount;
var price = itemDone.price;
count = count + 1;
var totalCast = price * count;
// 这里相当于直接修改了值,
itemDone.amount = count;
itemDone.total_cost = totalCast;
// var SS= "itemDone.amount";
// this.setData({SS :count});
// this.data.itemDone.amount = count;
console.log("this.setData.itemDone.amount"+itemDone.amount +" "+count);
}
}
//更新当前的数组
this.setData({
'done_dishes': this.data.done_dishes
});
},
});
```