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>
相关推荐
博主花神2 分钟前
【TypeScript】梳理
javascript·ubuntu·typescript
淡笑沐白6 分钟前
ECharts入门指南:数据可视化实战
前端·javascript·echarts
魔卡少女18 分钟前
Nginx配置代码化自动部署詹金斯/Github方案
前端·nginx·github
开发者如是说10 分钟前
可能是最好用的多语言管理工具
android·前端·后端
非科班Java出身GISer12 分钟前
ArcGIS JS 基础教程(1):地图初始化(含AMD/ESM两种引入方式)
javascript·arcgis·arcgis js·arcgis js 初始化·arcgis js 地图初始化
是上好佳佳佳呀12 分钟前
【前端(六)】HTML5 新特性笔记总结
前端·笔记·html5
北城笑笑28 分钟前
FPGA 与 市场主流芯片分类详解:SoC/CPU/GPU/DPU 等芯片核心特性与工程应用
前端·单片机·fpga开发·fpga
A923A1 小时前
【从零开始学 React | 第四章】useEffect和自定义Hook
前端·react.js·fetch·useeffect
ZC跨境爬虫1 小时前
批量爬取小说章节并优化排版(附完整可运行脚本)
前端·爬虫·python·自动化
ZC跨境爬虫1 小时前
海南大学交友平台登录页开发实战day4(解决python传输并读取登录信息的问题)
开发语言·前端·python·flask·html