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>
相关推荐
Highcharts.js几秒前
使用Highcharts与React集成 官网文档使用说明
前端·react.js·前端框架·react·highcharts·官方文档
这是个栗子几秒前
AI辅助编程(二) - 通译千问
前端·ai·通译千问
VT.馒头12 分钟前
【力扣】2625. 扁平化嵌套数组
前端·javascript·算法·leetcode·职场和发展·typescript
数研小生41 分钟前
Full Analysis of Taobao Item Detail API taobao.item.get
java·服务器·前端
Shirley~~1 小时前
Vue-skills的中文文档
前端·人工智能
毎天要喝八杯水1 小时前
搭建vue前端后端环境
前端·javascript·vue.js
计算机程序设计小李同学1 小时前
幼儿园信息管理系统的设计与实现
前端·bootstrap·html·毕业设计
雨季6662 小时前
Flutter 三端应用实战:OpenHarmony “极简手势轨迹球”——指尖与屏幕的诗意对话
开发语言·javascript·flutter
雨季6662 小时前
Flutter 三端应用实战:OpenHarmony “专注时光盒”——在碎片洪流中守护心流的数字容器
开发语言·前端·安全·flutter·交互
tao3556672 小时前
【用AI学前端】HTML-02-HTML 常用标签(基础)
前端·html