如何使用vue实现一个简易的购物车

先看效果图
这里写图片描述

直接上代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.0.2/css/bootstrap.min.css"/>
</head>
<body>

    <div id="app" class="container">
        <table class="table">
            <thead>
                <tr>
                    <th>产品编号</th>
                    <th>产品名字</th>
                    <th>购买数量</th>
                    <th>产品单价</th>
                    <th>产品总价</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item , index) in message">
                    <td @click="jia(index)">{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>
                        <button type="button" class="btn tn-primary" @click="subtract(index)">-</button>
                        <input type="text" v-model="item.quantity">
                        <button type="button" class="btn tn-primary" @click="add(index)">+</button>
                    </td>
                    <td>{{item.price | filtermoney}}</td>
                    <!--<td>{{arr[index].one}}</td>-->
                    <td>{{item.price*item.quantity | filtermoney}}</td>
                    <td>
                        <button type="button" class="btn btn-danger" @click="remove(index)">移除</button>
                    </td>
                </tr>
                <tr>
                    <td>总购买价            
                    </td>
                    <td>
                        {{animatenum | filtermoney}}
                    </td>
                    <td>总购买数量
                    </td>
                    <td>

                    </td>
                    <td colspan="2">
                        <button type="button" class="btn btn-danger" @click="empty()">清空购物车</button>
                    </td>
                </tr>
            </tbody>
        </table>

        <p v-if="message.length===0">您的购物车为空</p>
    </div>
    <script src="https://unpkg.com/tween.js@16.3.4"></script>
    <script src="https://cdn.bootcss.com/vue/2.2.3/vue.min.js"></script>
    <script>
        var vm=new Vue({
            el:"#app",
            data:{
                totalPrice:0,
                animatenum:0,
                message:[
                    {
                        id: 007,
                        name: 'iphone5s',
                        quantity: 3,
                        price: 4000
                   },{
                        id: 1340,
                        name: 'iphone5',
                        quantity: 9,
                        price: 3000
                   },{
                        id: 7758,
                        name: 'imac',
                        quantity: 4,
                        price: 7000
                   },{
                        id: 2017,
                        name: 'ipad',
                        quantity: 5,
                        price: 6000
                    }
                ]
            },
            watch:{
                toComput2:function(newValue,oldValue){
                    this.tween(newValue,oldValue);
                }
            },
            computed:{
                //计算总金额
                toComput2:function(){
                    var vm=this;
                    //每次进来要重置总金额
                    vm.totalPrice=0;
                    this.message.forEach(function(mess){
                        vm.totalPrice+=parseInt(mess.price*mess.quantity);
                    })
                    return this.totalPrice;
                }
            },
            filters:{
                filtermoney:function(value){
                    return '¥'+value ;
                }
            },
            mounted:function(){ 
                this.tween('97000','0');
            },
            methods:{
                //计算总数的方法为什么写在methods里面就不行?
                toComput:function(){
                    var vm=this;
                    vm.message.forEach(function(mess){
                        vm.totalPrice+=parseInt(mess.price*mess.quantity);
                    })
                    return vm.totalPrice;
                },
                add:function(index){
                    var vm=this;
                    vm.message[index].quantity++;
                },
                subtract:function(index){
                    var vm=this;
                    vm.message[index].quantity--;
                    if(vm.message[index].quantity<=0){
                        if (confirm("你确定移除该商品?")) {  
                            vm.message.splice(index,1)
                        } 
                    }

                },
                remove:function(index){
                    var vm=this;
                    if (confirm("你确定移除该商品?")) {  
                        vm.message.splice(index,1)
                    }  
                },
                empty:function(){
                    var vm=this;
                    vm.message.splice(0,vm.message.length);
                },
                jia:function(index){
                    var vm=this;
                    vm.arr[index].one++;
                },
                tween:function(newValue,oldValue){
                    var vm=this;
                    var twen=new TWEEN.Tween({animatenum:oldValue});
                    function animate() {
                        requestAnimationFrame(animate);         
                        TWEEN.update(); 
                    };
                    twen.to({animatenum:newValue},750);
                    twen.onUpdate(function(){
                    //toFixed();保留几位小数
                        vm.animatenum = this.animatenum.toFixed();
                    })
                    twen.start();
                    animate();
                }
            }
        });


    </script>   

</body>
</html>
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
由于我是AI语言模型,无法直接实现代码,以下是用Vue框架实现购物车页面的一些指导: 1. 首先,需要创建一个Vue实例。可以通过Vue CLI或手动引入Vue库来创建。 2. 创建一个包含购物车商品信息的数组。每个商品应该包括名称、价格、数量和图片等信息。 3. 在Vue实例中定义一个data属性,将创建的购物车商品数组赋值给它。例如: ``` data: { cartItems: [ { name: '商品1', price: 10, quantity: 2, image: 'image1.jpg' }, { name: '商品2', price: 20, quantity: 1, image: 'image2.jpg' }, { name: '商品3', price: 30, quantity: 3, image: 'image3.jpg' }, ] } ``` 4. 在Vue模板中使用v-for指令循环渲染购物车商品列表。例如: ``` <div v-for="item in cartItems" :key="item.name"> <img :src="item.image"> <h3>{{ item.name }}</h3> <p>单价:{{ item.price }}元</p> <p>数量:{{ item.quantity }}</p> <p>小计:{{ item.price * item.quantity }}元</p> </div> ``` 5. 添加购物车商品数量的增减功能。可以使用v-model指令绑定商品数量的值,然后通过方法实现增减功能。例如: ``` <div v-for="item in cartItems" :key="item.name"> ... <button @click="decreaseQuantity(item)">-</button> <input type="number" v-model="item.quantity"> <button @click="increaseQuantity(item)">+</button> ... </div> methods: { increaseQuantity(item) { item.quantity++; }, decreaseQuantity(item) { if (item.quantity > 1) { item.quantity--; } } } ``` 6. 添加总价和结算功能。可以通过计算属性计算购物车商品的总价,并在模板中显示。然后添加一个结算按钮,点击后触发一个方法,弹出提示框或跳转到结算页面。例如: ``` <div> ... <p>总价:{{ totalPrice }}元</p> <button @click="checkout">结算</button> </div> computed: { totalPrice() { return this.cartItems.reduce((total, item) => { return total + item.price * item.quantity; }, 0); } }, methods: { checkout() { // 弹出提示框或跳转到结算页面 } } ``` 以上是用Vue框架实现购物车页面的基本步骤和代码示例。具体实现可能会因需求和实际情况有所不同。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值