Vue项目(购物车)

目录

购物车效果展示:

购物车代码:


购物车效果展示:

此项目添加、修改、删除数据的地方都写了浏览器都会把它存储起来

下次运行项目时会把浏览器数据拿出来并在页面展示

Video_20230816145047

购物车代码:

复制完代码,需改下script中引入的vue文件地址;可直接使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <div>
            <form action=""> 
                商品名称:<input type="text" v-model="productName" name="productName">
                商品单价:<input type="text" v-model="productPrice" name="productPrice">
                <input type="button" value="添加商品" @click="addProduct">
            </form>
        </div>
        <ul>
            <li v-for="(pro,index) in productList" :key="index">
                商品名称:{{pro.productName}}=========商品单价:{{pro.productPrice}}&nbsp;&nbsp;&nbsp;
                <button type="button" @click="addProToCart(index)">添加到购物车</button>
                <button type="button" @click="deleteProToCart(index)">删除此商品</button>
            </li>
        </ul>
        <cart :cartlist="cartList"></cart>
    </div>

    <template id="cartHtml">
        <div>
            <table border="1">
                <tr>
                    <td>全选<input type="checkbox" @change="checkActive" id="isCheck"></td>
                    <td>商品名称</td>
                    <td>商品单价</td>
                    <td>商品数量</td>
                    <td>商品价格</td>
                </tr>
                <tr v-for="(pro,index) in cartlist" :key="index">
                    <td><input type="checkbox" v-model="pro.active" @change="ziCheck"></td>
                    <td>{{pro.productName}}</td>
                    <td>{{pro.productPrice}}</td>
                    <td>
                        <button type="button" @click="reduceProNum(index)">-</button>
                        {{pro.productNum}}
                        <button type="button" @click="addProNum(index)">+</button>
                    </td>
                    <td>{{pro.productPrice*pro.productNum}}</td>
                </tr>
                <tr>
                    <td colspan="3">选中的商品:{{activeNum}}/{{cartlist.length}}</td>
                    <td colspan="2">总价格:{{totalPrice}}</td>
                </tr>
            </table>
        </div>
    </template>

</body>
<script src="../js/vue2.7.js"></script><!--根据自己的vue文件地址填写-->
<script>
    //创建一个购物车子组件
    var cart={
        template:"#cartHtml",
        props:["cartlist"],
        methods:{
            addProNum(index){
                let product =this.cartlist[index];
                product.productNum++
                localStorage.setItem('cartList', JSON.stringify(this.cartlist));
            },
            reduceProNum(index){
                let product =this.cartlist[index];
                //判断商品数量是否为一
                if (product.productNum==1) {
                    this.cartlist.splice(index,1)//为一,在数组中删除掉
                    //删除完后把数据放在浏览器里面把key值设置为cartList
                    localStorage.setItem('cartList', JSON.stringify(this.cartlist));
                }else{
                    product.productNum--
                    //减完之后把数据放在浏览器里面把key值设置为cartList
                    localStorage.setItem('cartList', JSON.stringify(this.cartlist));
                }
            },
            checkActive(){
                if(document.getElementById("isCheck").checked){
                    for(var i=0;i<this.cartlist.length;i++){
                        this.cartlist[i].active=true;
                    }
                    //全选为true后把数据放在浏览器里面把key值设置为cartList
                    localStorage.setItem('cartList', JSON.stringify(this.cartlist));
                }else{
                    for(var i=0;i<this.cartlist.length;i++){
                        this.cartlist[i].active=false;
                    }
                    //全选为false后把数据放在浏览器里面把key值设置为cartList
                    localStorage.setItem('cartList', JSON.stringify(this.cartlist));
                }
            },
            ziCheck(){
                //当多选框变化时把数据放在浏览器里面把key值设置为cartList
                localStorage.setItem('cartList', JSON.stringify(this.cartlist));
            },
        },
        computed:{
            //计算购物车商品总和
            activeNum(){
                let activeProductList=this.cartlist.filter(item=>{
                    return item.active
                })
                return activeProductList.length;
            },
            //计算购物车商品的总价格
            totalPrice(){
                let result=0;
                for(pro of this.cartlist){
                    if(pro.active){
                        result=result+pro.productPrice*pro.productNum
                    }
                }
                return result
            }
        },
        updated() {
            //当多选框都为true全选后的多选框为true
            var isActive=this.cartlist.every(c => c.active)
            if (isActive) {
                document.getElementById("isCheck").checked=true
            } else {
                document.getElementById("isCheck").checked=false
            }
        },
    }

    let app=new Vue({
        el:"#app",
        data() {
            return {
                productName:'',
                productPrice:'',
                productList:[],
                cartList:[]
            }
        },
        methods: {
            addProduct(){
                let isnameOk=true;
                let ispriceOk=true;
                if (this.productName=="") {
                    isnameOk=false
                }
                if(isNaN(this.productPrice) || this.productPrice<=0){
                    ispriceOk=false;
                }
                if(isnameOk && ispriceOk){
                    //查找新增的商品是否存在商品列表中,如果不存在返回-1
                    let findindex=this.productList.findIndex(item=>{
                        return item.productName==this.productName
                    })
                    //判断商品列表中是否存在新增的商品
                    if(findindex==-1){
                        //把新商品添加到商品列表中
                        this.productList.push({productName:this.productName,productPrice:this.productPrice})
                        //把数据放在浏览器里面把key值设置为productList
                        localStorage.setItem('productList', JSON.stringify(this.productList));
                        //添加完表单中的输入框调为空
                        this.productName='';
                        this.productPrice='';
                    }else{
                        alert("此商品已经存在商品列表!")//商品已存在,给出提示
                    }
                }else{
                    alert("请输入合适的商品名称及单价")
                }
            },
            addProToCart(index){
                let newproduct=this.productList[index];//根据下标从商品列表里面取出商品
                //从购物车列表中查找,是否存在新的商品,如果找到返回购物车的商品
                let product= this.cartList.find(item=>{
                    return item.productName==newproduct.productName
                })
                if (product) {
                    //如果有对应的商品则数量加一
                    product.productNum++
                }else{
                    //没有对应的商品就添加商品到购物车
                    this.cartList.push({
                        productName:newproduct.productName,
                        productPrice:newproduct.productPrice,
                        productNum:1,
                        active:true
                    })
                    //把数据放在浏览器里面把key值设置为cartList
                    localStorage.setItem('cartList', JSON.stringify(this.cartList));
                }
            },
            deleteProToCart(index){
                let isOk=confirm("是否删除此商品!")
                if(isOk){
                    this.productList.splice(index,1)
                }
                //把数据放在浏览器里面把key值设置为productList
                localStorage.setItem('productList', JSON.stringify(this.productList));
            }
        },
        //生命周期钩子,部署完后执行从浏览器中把数据拿出来
        mounted(){
            for(pro of JSON.parse(localStorage.getItem("productList"))){
                this.productList.push({productName:pro.productName,productPrice:pro.productPrice});
            }
            for(pro of JSON.parse(localStorage.getItem("cartList"))){
                this.cartList.push({productName:pro.productName,productPrice:pro.productPrice,productNum:pro.productNum,active:pro.active});
            }
        },
        components:{
            cart
        },
    })
</script>
</html>
  • 25
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 27
    评论
Spring Boot 是一个用于快速构建 Java 应用程序的开发框架,而 Vue 是一种用于构建用户界面的 JavaScript 框架。 图书商城项目是一个典型的电子商务应用,主要功能包括图书的浏览、搜索、购买、下单以及管理等。通过结合 Spring Boot 和 Vue,可以很好地实现这个项目。 首先,在后端使用 Spring Boot 来搭建图书商城的服务端。Spring Boot 提供了便捷的配置和快速的开发环境,可以快速构建出稳定的服务器。通过 Spring Boot,可以实现用户的认证与授权、订单的管理、图书信息的存取等功能。同时,Spring Boot 还提供了丰富的插件和框架,比如 Spring Security,可以很好地保护用户的隐私与数据安全。 其次,在前端使用 Vue 来构建图书商城的客户端。Vue 提供了开发用户界面的便利性和灵活性,可以通过组件化开发思想来构建交互式的界面。通过 Vue,可以实现用户的注册与登录、图书的展示与搜索、购物车的管理以及订单的提交等功能。同时,Vue 还提供了丰富的插件和库,比如 Vue Router 和 Axios,可以方便地实现页面的路由切换和数据的请求。 最后,通过 Spring Boot 和 Vue 的结合,可以实现前后端的无缝集成。通过 RESTful API,后端可以提供给前端所需的数据和操作接口。前端可以通过 Vue 框架来发起请求并展示数据,同时也可以通过组件化的方式进行界面的构建和复用。通过这种方式,实现了前后端的解耦,提高了开发效率和代码的可维护性。 综上所述,使用 Spring Boot 和 Vue 可以很好地实现图书商城项目,提供了强大的后端支持和灵活的前端开发环境。这个结合可以帮助开发者快速构建高质量的图书商城应用,并且能够满足用户的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值