Vue实现简单的购物车

Vue方法的优劣探讨
博客围绕Vue相关方法展开,提及了一种被认为是‘最垃圾’的方法,同时也指出存在略有改进的情况,聚焦于Vue方法的不同表现。
最垃圾的方法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <table border=1 align="center" width="40%">
            <tr align="center">
                <th>
                    商品名称
                </th>
                <th>
                    商品图片
                </th>
                <th>
                    商品单价
                </th>
                <th>
                    数量商品
                </th>
            </tr>
            <tr  align="center">
                <td>尿不湿</td>
                <td>
                    <img :src="imgurl1" alt="" width="50px">
                </td>
                <td>{{price1}}</td>
                <td>
                    <button @click="add1">+</button>
                    <input type="text" id="nbs" size="1" :value="value1">
                    <button @click="dele1">-</button>
                </td>
            </tr>
            <tr align="center">
                <td>酱油</td>
                <td>
                        <img v-bind:src="imgurl2" alt="" width="50px">
                </td>
                <td>{{price2}}</td>
                <td>
                    <button @click="add2">+</button>
                    <input type="text" id="nbs" size="1" :value="value2">
                    <button @click="dele2">-</button>
                </td>
            </tr>
            <tr align="center">
                <td>毛衣</td>
                <td>
                        <img :src="imgurl3" alt="" width="50px">
                </td>
                <td>{{price3}}</td>
                <td>
                        <button @click="add3">+</button>
                        <input type="text" id="nbs" size="1" :value="value3">
                        <button @click="dele3">-</button>
                </td>
            </tr>
            <tr>
                <td colspan="4" align="right">
                    总价: <input type="text" id="zj" size="5" :value="all">                
                </td>
            </tr>
        </table>

    </div>

    <script>

    const app=new Vue({
        el:"#app",
        data:{
            imgurl1:"icon/nbs.png",
            imgurl2:"icon/jy.png",
            imgurl3:"icon/my.png",
            value1:0,
            value2:0,
            value3:0,
            all:0,
            price1:15,
            price2:13,
            price3:42
        },
        methods:{
            add1:function(){
                
               this.value1= this.value1+1,
               this.all+=this.price1
            },
            add2:function(){
               this.value2= this.value2+1,
               this.all+=this.price2
            },
            add3:function(){
               this.value3= this.value3+1,
               this.all+=this.price3
            },
            dele1:function(){
                if(this.value1>=1){
                    this.value1= this.value1-1,
                    this.all-=this.price1
                }
               
            },
            dele2:function(){
                if(this.value2>=1){
                    this.value2= this.value2-1,
                    this.all-=this.price2
                }
            },
            dele3:function(){
                if(this.value3>=1){
                    this.value3= this.value3-1,
                    this.all-=this.price3
                }
            }
            
        }
    })
    
    </script>



</body>
</html>

略有改进

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
        <div id="app">
                <table border=1 align="center" width="40%">
                    <tr align="center">
                        <th>
                            商品名称
                        </th>
                        <th>
                            商品图片
                        </th>
                        <th>
                            商品单价
                        </th>
                        <th>
                            数量商品
                        </th>
                    </tr>
                    <tr v-for="item in items" align="center">
                        <td>
                            {{item.name}}
                        </td>
                        <td>
                            <img v-bind:src="item.img" alt="" width="50px">
                        </td>
                        <td>
                            {{item.danjia}}
                        </td>
                        <td>
                                <button @click="item.number+=1">+</button>
                                {{item.number}}
                                <button @click="item.number<=1 ? item.number=0 : item.number-=1">-</button>
                        </td>
                    </tr>
                    <tr align="right">
                        <td colspan="4">
                            总价: {{alll}}
                        </td>
                    </tr>
                </table>
        </div>
<script>

    const app=new Vue({
        el:"#app",
        data:{
            all:0,
            items:[
                {name:"尿不湿",img:"icon/nbs.png",danjia:15,number:0},
                {name:"酱油",img:"icon/jy.png",danjia:6,number:0},
                {name:"毛衣",img:"icon/my.png",danjia:45,number:0}
            ]

        },
        computed: {
            
            alll:function(){
               let num=0;
               for (let i = 0; i <this.items.length; i++) {
                    num+=this.items[i].number*this.items[i].danjia;
                   
               }

               return num;
            }
             
        },
        })
       
</script>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值