主要逻辑

    1. checkAll: {
    2. get() {
    3. return this.products.every(item => item.isSelected);
    4. },
    5. set(val) {
    6. this.products.forEach(item => (item.isSelected = val));
    7. }
    8. },
    1. <template>
    2. <div class="content">
    3. <h2>购物车</h2>
    4. <van-swipe :autoplay="3000" indicator-color="white">
    5. <van-swipe-item v-for="(item,i) of products" :key="i">
    6. <img class="swiper" :src="item.productCover" alt @click="toggle(i)" />
    7. </van-swipe-item>
    8. </van-swipe>
    9. <div class="item" v-for="item of products" :key="item.id">
    10. <van-checkbox class="checkAll" v-model="item.isSelected"></van-checkbox>
    11. <van-stepper
    12. class="num"
    13. v-model="item.productCount"
    14. input-width="20px"
    15. button-size="20px"
    16. integer
    17. />
    18. <van-card
    19. :price="item.productPrice"
    20. :desc="item.productInfo"
    21. :title="item.productName"
    22. :thumb="item.productCover"
    23. >
    24. <div slot="bottom">小计:{{item.productPrice*item.productCount | money(2)}}</div>
    25. <div slot="footer">
    26. <van-button size="mini" type="danger" @click="handledelete(item)">删除</van-button>
    27. </div>
    28. </van-card>
    29. </div>
    30. <van-submit-bar :price="sum" button-text="提交订单">
    31. <van-checkbox v-model="checkAll">全选</van-checkbox>
    32. </van-submit-bar>
    33. </div>
    34. </template>
    35. <script>
    36. import { Dialog } from 'vant';
    37. import { ImagePreview } from "vant";
    38. export default {
    39. name: "cart",
    40. data() {
    41. return {
    42. products: [],
    43. img: []
    44. };
    45. },
    46. mounted() {
    47. this.axios.get("http://yapi.demo.qunar.com/mock/34614/").then(res => {
    48. this.products = res.data;
    49. this.products.forEach(item => {
    50. this.img.push(item.productCover);
    51. });
    52. });
    53. },
    54. computed: {
    55. checkAll: {
    56. get() {
    57. return this.products.every(item => item.isSelected);
    58. },
    59. set(val) {
    60. this.products.forEach(item => (item.isSelected = val));
    61. }
    62. },
    63. sum() {
    64. var total = 0;
    65. for (let i = 0; i < this.products.length; i++) {
    66. if (this.products[i].isSelected) {
    67. total =
    68. total +
    69. this.products[i].productCount * this.products[i].productPrice;
    70. } else {
    71. continue;
    72. }
    73. }
    74. return total * 100;
    75. }
    76. },
    77. methods: {
    78. handledelete(item) {
    79. Dialog.confirm({
    80. title: "提示",
    81. message: "确认删除"
    82. })
    83. .then(() => {
    84. var products = this.products.filter(value => value !== item);
    85. this.products = products;
    86. })
    87. .catch(() => {
    88. // on cancel
    89. });
    90. },
    91. toggle(index) {
    92. ImagePreview({
    93. images: this.img,
    94. startPosition: index
    95. });
    96. }
    97. }
    98. };
    99. </script>