vue3展示列表和加减求和

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app"></div>
    <template id="root">
       <table>
           <tr>
               <th>序号</th>
               <th>商品名称</th>
               <th>单价</th>
               <th>金额</th>
               <th>数量</th>
           </tr>
           <tr v-for="(item,key) in shop">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>价格:{{item.prices}}</td> 
                <td>{{item.number}}</td>
                <td>
                    <button @click="jia(key)">+</button>
                    <button @click="jian(key)">-</button>
                </td>
           </tr>
       </table>
            
    </template>
    <script>
       Vue.createApp({
        template:`#root`,
        data() {
            return {
                shop:[
                    {id:1,name:'Java无难事',price:188,prices:188,number:1},
                    {id:2,name:'VC++深入详解',price:168,prices:168,number:1},
                    {id:3,name:'Serve/Jsp深入详解',price:139,prices:139,number:1}
                ],
               
            }
        },
        methods: {
            jia(key){
                this.shop[key].number+=1;
                this.shop[key].prices=this.shop[key].price*this.shop[key].number;
            },
            jian(key){
                if(this.shop[key].number>0){
                    this.shop[key].number-=1;
                    this.shop[key].prices=this.shop[key].price*this.shop[key].number;
                }
            },
        },
       }).mount('#app')
   </script>
</body>
</html>

key的传递极为重要,它将确定你的值是否能被获取

效果图:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值