Vue3_列表渲染

测试1

测试2

clike 复制代码
<script setup >
import {ref,reactive} from 'vue'
let sub = ref("科目")

let carts = reactive(
  [
    {
      name:"可乐",
      price:3,
      number:10
    },
    {
      name:"薯片",
      price:6,
      number:12
    },
    {
      name:"炸鸡",
      price:12,
      number:2
    }
  ]
)

// 计算购物车总金额的函数
function compute(){
  let total = 0
  for(let index in carts){
    total += carts[index].price * carts[index].number
  }
  return total
}


//  从购物车中移除购物项的方法
function removeCart(index){
  carts.splice(index,1)
}

function clearCart(){
  carts.splice(0,carts.length)
}

</script>
<template>

<div>

  <h1>您的购物车如下</h1>

  <table border="1px">
    <thead>
      <tr>
        <th>序号</th>
        <th>名称</th>
        <th>价格</th>
        <th>数量</th>
        <th>小计</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody v-if="carts.length > 0">
      <tr v-for="(cart,index) in carts" :key = "index">
        <td>{{index+1}}</td>
        <td>{{cart.name}}</td>
        <td>{{cart.price}}</td>
        <td>{{cart.number}}</td>
        <td>{{cart.price * cart.number}}</td>
        <td>
          <button @click="removeCart(index)">删除</button>
        </td>
      </tr>
    </tbody>
    <tbody v-else>
      <tr>
        <td colspan="6">
          购物车空了
        </td>
      </tr>
      
    </tbody>

  </table>
  购物车总金额 {{compute()}} 元<br>
  <button @click="clearCart()">一键清空购物车</button>

   
</div>
</template>

<style scoped>
</style>
相关推荐
vx_Biye_Design14 小时前
springboot小学生英语学习APP62773-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·学习·课程设计
vx_Biye_Design15 小时前
expressDeepSeek社团咨询助手的学生社团管理系统60746-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·课程设计·express
传人15 小时前
悬停旋转放大和位移效果如何写
前端·javascript·css
vx_Biye_Design15 小时前
springboot宠物领养与救助平台64334-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·mysql·课程设计·宠物
vx_Biye_Design16 小时前
django就业信息推荐系统61953-计算机课程设计、毕业设计
java·vue.js·spring boot·python·架构·django·课程设计
羲云16 小时前
给 Vue 页面加个 Markdown 编辑器:ME.js 的接入、图片粘贴与音视频
javascript·vue.js
zhangzeyuaaa16 小时前
Ruby 方法参数完全指南:默认值、可变参数与关键字参数
前端·python·ruby
粥里有勺糖16 小时前
视野修炼-技术周刊第135期 | 鼠标跟随宠物
前端·github·aigc
粥里有勺糖16 小时前
拿 Codex 协助清理磁盘,Nice!
前端·后端·github
专业程序开发源16 小时前
springboot高校学生社团管理系统74810-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计