Blogger Information
Blog 48
fans 3
comment 1
visits 30654
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
条件渲染与列表渲染及改写购物车案例
吴长清
Original
315 people have browsed it

1.条件渲染

  1. <div class="app">
  2. <span>你现在的段位是:</span>
  3. <h2 v-if="point>=0 && point<400">{{grade[0]}}</h2>
  4. <h2 v-else-if="point>=400 && point<800">{{grade[1]}}</h2>
  5. <h2 v-else-if="point>=800 && point<1200">{{grade[2]}}</h2>
  6. <h2 v-else-if="point>=1200 && point<1600">{{grade[3]}}</h2>
  7. <h2 v-else-if="point>=1600 && point<2000">{{grade[4]}}</h2>
  8. <h2 v-else-if="point>=2000 && point<2400">{{grade[5]}}</h2>
  9. <h2 v-else-if="point>=2400 && point<2800">{{grade[6]}}</h2>
  10. <h2 v-else-if="point>=2800 && point<3200">{{grade[7]}}</h2>
  11. <h2 v-if="point>=3200">{{grade[8]}}</h2>
  12. </div>
  13. <script>
  14. Vue.createApp({
  15. data() {
  16. return {
  17. // 段位
  18. grade: [
  19. "坚韧黑铁",
  20. "英勇黄铜",
  21. "不屈白银",
  22. "荣耀黄金",
  23. "华贵铂金",
  24. "璀璨钻石",
  25. "超凡大师",
  26. "傲视宗师",
  27. "最强王者",
  28. ],
  29. // 积分
  30. point: 3200,
  31. };
  32. },
  33. }).mount(".app");
  34. </script>

2.列表渲染

  1. <div class="app">
  2. <span>列表渲染: 数组</span>
  3. <ul>
  4. <li v-for="(item,index) of names" :key="index">{{index}} => {{item}}</li>
  5. </ul>
  6. <hr />
  7. <span>列表渲染: 对象数组</span>
  8. <ul>
  9. <li v-for="(item,index) of userInfo" :key="index">
  10. 索引:{{index}}=>( 姓名:{{item.name}} ) (性别:{{item.sex}}) (邮箱:{{item.email}})
  11. </li>
  12. </ul>
  13. </div>
  14. <script>
  15. const app = Vue.createApp({
  16. data() {
  17. return {
  18. names: ["张三", "李四", "王五", "麻子"],
  19. userInfo: [
  20. { name: "张三", sex: "男", email: "zhangsan@qq.com" },
  21. { name: "李四", sex: "女", email: "lisi@qq.com" },
  22. { name: "王五", sex: "男", email: "wangwu@qq.com" },
  23. { name: "麻子", sex: "男", email: "mazi@qq.com" },
  24. ],
  25. };
  26. },
  27. }).mount(".app");
  28. </script>

3.购物车案例

  1. <div class="app">
  2. <div class="selectAll">
  3. <!-- v-model:双向绑定checked的状态 -->
  4. <input type="checkbox" class="check-all" name="check-all" v-model="checkAllStatus" />
  5. <label for="check-all">全选</label>
  6. </div>
  7. <ul class="list">
  8. <li>
  9. <span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span>
  10. </li>
  11. <!-- v-for 遍历 商品列表 -->
  12. <li v-for="(item,index) of prodInfo">
  13. <input type="checkbox" v-model="item.status" />
  14. <span class="content">{{item.name}}</span>
  15. <!-- v-model:双向绑定num的值 -->
  16. <input type="number" v-model="item.num" min="1" class="num" />
  17. <span class="price">{{item.price}}</span>
  18. <span class="amount">{{amount[index]}}</span>
  19. </li>
  20. <li>
  21. <span>总计:</span>
  22. <span class="total-num">数量: {{totalNum}}</span>
  23. <span class="total-amount">总金额: {{totalAmount}}</span>
  24. </li>
  25. <li>
  26. <span> 优惠:</span>
  27. <span class="difAmount">优惠: {{difAmount}}</span>
  28. <span class="realAmount">最终价: {{realAmount}}</span>
  29. </li>
  30. </ul>
  31. <!-- 3. 结算按钮 -->
  32. <button class="account">结算</button>
  33. </div>
  1. const app = Vue.createApp({
  2. data() {
  3. return {
  4. // 商品信息
  5. prodInfo: [
  6. { name: "手机", num: 1, price: 100, status: false },
  7. { name: "电脑", num: 2, price: 200, status: false },
  8. { name: "相机", num: 3, price: 300, status: false },
  9. ],
  10. difAmount: 0,
  11. realAmount: 0,
  12. };
  13. },
  14. // 计算属性
  15. computed: {
  16. checkAllStatus: {
  17. get() {
  18. // 列表中的复选框需要全部满足时 改变全选框的状态
  19. return this.prodInfo.every((item) => item.status === true);
  20. },
  21. set(ev) {
  22. // 使列表中的input的状态与全选框的状态保持一致
  23. this.prodInfo.forEach((item) => (item.status = ev));
  24. },
  25. },
  26. // 金额
  27. amount: {
  28. get() {
  29. return this.prodInfo.map((item) => item.price * item.num);
  30. },
  31. set(v) {},
  32. },
  33. //总数量
  34. totalNum: {
  35. get() {
  36. // 得到选中的数据
  37. let checkData = this.prodInfo.filter((item) => item.status === true);
  38. // 得到选中的数量数组
  39. let numArr = checkData.map((item) => item.num);
  40. return numArr.length === 0 ? 0 : numArr.reduce((acc, cur) => acc + cur);
  41. },
  42. set(v) {},
  43. },
  44. // 总金额
  45. totalAmount: {
  46. get() {
  47. // 得到选中的数据
  48. let checkData = this.prodInfo.filter((item) => item.status === true);
  49. // 得到选中的金额数组
  50. let amountArr = checkData.map((item) => item.num * item.price);
  51. return amountArr.length === 0 ? 0 : amountArr.reduce((acc, cur) => acc + cur);
  52. },
  53. set(v) {},
  54. },
  55. },
  56. // 侦听器属性
  57. watch: {
  58. totalAmount(current, origin) {
  59. // 根据当前金额确定打折
  60. switch (true) {
  61. // 1000-2000: 9折
  62. case current >= 1000 && current < 2000:
  63. this.discount = 0.9;
  64. break;
  65. // 2000 -> 3000 : 8折
  66. case current >= 2000 && current < 3000:
  67. this.discount = 0.8;
  68. break;
  69. // 3000 -> 4000 : 7折
  70. case current >= 3000 && current < 4000:
  71. this.discount = 0.7;
  72. break;
  73. // 4000 -> 5000 : 6折
  74. case current >= 4000 && current < 5000:
  75. this.discount = 0.6;
  76. break;
  77. // 5000 : 5折
  78. case current >= 5000:
  79. this.discount = 0.5;
  80. break;
  81. // 默认不打折
  82. default:
  83. this.discount = 1;
  84. }
  85. // 实付金额 = 原始金额 * 折扣率
  86. this.realAmount = this.totalAmount * this.discount;
  87. // 优惠金额(差价) = 原始金额 - 实付金额
  88. this.difAmount = this.totalAmount - this.realAmount;
  89. },
  90. },
  91. }).mount(".app");
  92. </script>
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