这个文件在page 同名的js下面(https://opendocs.alipay.com/mini/framework/page-detail

    1. ```java
    2. //变量定义
    3. var dish_all_cost = 0;
    4. var dish_all_menu = "";
    5. var current_bottom_menu_show = false;
    6. //
    7. Page({
    8. //页面的数据
    9. data: {
    10. dish_count: 0,
    11. dish_menu: "当前购物车是空的",
    12. bottom_menu_show: false,
    13. done_dishes: [
    14. {
    15. id: 1,
    16. name: "可乐",
    17. },
    18. {
    19. id: 2,
    20. name: "薯条",
    21. },
    22. ]
    23. ,
    24. },
    25. //这里定义的事件
    26. handlebottomView(e) {
    27. console.log("底部菜单被点击了");
    28. if (!current_bottom_menu_show) {
    29. this.setData({ bottom_menu_show: true });
    30. } else {
    31. this.setData({ bottom_menu_show: false });
    32. }
    33. current_bottom_menu_show = !current_bottom_menu_show;
    34. },
    35. handleDish(e) {
    36. // 获取到当前事件
    37. const dishInfo = e.target.dataset.dishInfo || {};
    38. console.info("CLICK" + dishInfo.name);
    39. dish_all_cost = dish_all_cost + dishInfo.price;
    40. dish_all_menu = dish_all_menu + dishInfo.name + ";";
    41. this.setData({ dish_menu: dish_all_menu });
    42. this.setData({ dish_count: dish_all_cost });
    43. var valueAble = false;
    44. console.log("dishInfo"+dishInfo.id);
    45. //https://www.cnblogs.com/m2maomao/p/7743143.html
    46. //这里的循环为什么要of
    47. //总结来说:for in总是得到对像的key或数组,字符串的下标,而for of和forEach一样,是直接得到值
    48. for (var item of this.data.dishes) {
    49. console.log("item"+item.id);
    50. if( item.id == dishInfo.id){
    51. valueAble = true;
    52. break;
    53. } else {
    54. continue;
    55. }
    56. }
    57. if(!valueAble){
    58. console.log("当前点击不合法");
    59. return;
    60. } else {
    61. console.log("当前点击合法");
    62. }
    63. for (var itemDone of this.data.done_dishes){
    64. if(itemDone.id == dishInfo.id) {
    65. var count = itemDone.amount;
    66. var price = itemDone.price;
    67. count = count + 1;
    68. var totalCast = price * count;
    69. // 这里相当于直接修改了值,
    70. itemDone.amount = count;
    71. itemDone.total_cost = totalCast;
    72. // var SS= "itemDone.amount";
    73. // this.setData({SS :count});
    74. // this.data.itemDone.amount = count;
    75. console.log("this.setData.itemDone.amount"+itemDone.amount +" "+count);
    76. }
    77. }
    78. //更新当前的数组
    79. this.setData({
    80. 'done_dishes': this.data.done_dishes
    81. });
    82. },
    83. });

    ```