Blogger Information
Blog 43
fans 4
comment 0
visits 19383
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
条件渲染/循环渲染/vue改写购物车
汇享科技
Original
373 people have browsed it

条件渲染/循环渲染

l6g8rkd4.png

  • v-if:根据条件是否进行渲染
  • v-for:将数据循环渲染到页面中

vue改写购物车

  • 效果图:
  • 代码部分:
  1. <div class="box">
  2. <!-- 1. 全选按钮 -->
  3. <div class="selectAll">
  4. <!-- 通过双向绑定v-model来制定全选按钮的状态 -->
  5. <input
  6. type="checkbox"
  7. class="check-all"
  8. name="check-all"
  9. v-model="checkAll"
  10. />
  11. <label for="check-all">全选</label>
  12. </div>
  13. <!-- 2. 商品列表 -->
  14. <ul class="list">
  15. <li>
  16. <span>选择</span><span>品名</span><span>数量</span><span>单价</span
  17. ><span>金额</span>
  18. </li>
  19. <!-- 通过v-for渲染数据到页面中 -->
  20. <li v-for="(v,k) of shuju" :key="k">
  21. <input type="checkbox" v-model="v.zhuangtai" />
  22. <span class="content">{{v.name}}</span>
  23. <input type="number" v-model="v.num" min="1" class="num" />
  24. <span class="price">{{v.price}}</span>
  25. <!-- 商品单个的金额直接计算 -->
  26. <span class="amount">{{v.num*v.price}}</span>
  27. </li>
  28. <li>
  29. <span>总计:</span>
  30. <span class="total-num">总数量:{{shuliang}}</span>
  31. <span class="total-amount">总金额:{{zongjia}}</span>
  32. </li>
  33. </ul>
  34. <!-- 3. 结算按钮 -->
  35. <button class="account">结算</button>
  36. </div>
  1. //创建vue实例
  2. const app = Vue.createApp({
  3. data() {
  4. return {
  5. //数据列表 zhaungtai是按钮的状态为true是选中默认是false
  6. shuju: [
  7. {
  8. name: "手机",
  9. price: 100,
  10. num: 1,
  11. zhuangtai: false,
  12. },
  13. {
  14. name: "电脑",
  15. price: 200,
  16. num: 1,
  17. zhuangtai: false,
  18. },
  19. {
  20. name: "相机",
  21. price: 300,
  22. num: 1,
  23. zhuangtai: false,
  24. },
  25. ],
  26. };
  27. },
  28. //计算属性
  29. computed: {
  30. // 设置按钮选中状态
  31. checkAll: {
  32. get() {
  33. //将数据用filter返回满足条件的数组 这个数组的只用这个数组的长度
  34. let arr = this.shuju.filter((item) => item.zhuangtai === true);
  35. // console.log(arr);
  36. // console.log(arr.length);
  37. //当数组的长度等于数据列表的长度的时候返回true这时候全选按钮的状态为true否则就返回false
  38. if (arr.length == this.shuju.length) {
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. },
  44. set(e) {
  45. //e是当前全选按钮的状态
  46. // console.log(e);
  47. //如果全选按钮状态为true
  48. if (e) {
  49. //将所有单选按钮的状态设置为true
  50. this.shuju.map((item) => (item.zhuangtai = true));
  51. } else {
  52. //将所有单选按钮的状态设置为false
  53. this.shuju.forEach((item) => (item.zhuangtai = false));
  54. }
  55. },
  56. },
  57. //计算总金额
  58. zongjia() {
  59. //先定义一个变量接收结果
  60. let sum = 0;
  61. //遍历数据
  62. this.shuju.forEach((item) => {
  63. //判断状态是否为true
  64. if (item.zhuangtai) {
  65. // 拿到状态为true的商品的数量和单价循环进行相加赋给sum
  66. sum += item.num * item.price;
  67. }
  68. });
  69. //将结果返回此时这个sum就是总金额
  70. return sum;
  71. },
  72. //计算总数量
  73. shuliang() {
  74. let lsum = 0;
  75. //遍历数据
  76. this.shuju.forEach((item) => {
  77. //根据状态拿到为true的数量循环相加赋值给lsum
  78. if (item.zhuangtai) {
  79. lsum += item.num;
  80. }
  81. });
  82. //返回lsum这个结果就是总数量
  83. return lsum;
  84. },
  85. },
  86. //生命周期页面创建自动初始化
  87. mounted() {
  88. //初始化商品数量,默认为1
  89. this.sjnum = 1;
  90. this.dnnum = 1;
  91. this.xjnum = 1;
  92. },
  93. //将实例挂载到挂载点
  94. }).mount(".box");
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post