基于Vue2的购物车

 main.js

import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
Vue.config.productionTip = false
axios.defaults.baseURL = "http://localhost:3000/"
Vue.prototype.$axios = axios
new Vue({
  render: h => h(App),
}).$mount('#app')

Vue.$axios

准备好一个接口,我自己用的是express搭建的,也可以用其他的,用axios来获取数据

根组件App.vue 导入每个子组件

<template>
  <div>
    <my-header color="#fff" tit="购物车" bgcolor="blue"></my-header>
    <div class="main">
      <MyGoods v-for="obj in list" :key="obj.id" :lists="obj"></MyGoods>
    </div>
    <MyFooter :num="allnum" :price="allPrice" :lists="list"></MyFooter>
  </div>
</template>

<script>
 import MyHeader from './components/MyHeader'
import MyGoods from './components/MyGoods'
import MyFooter from './components/MyFooter'
export default {
  components: {
    MyHeader,
    MyGoods,
    MyFooter
  },
  data () {
    return {
        list: []
    }
  },
  created () {
      this.$axios({
        url: '/api/cart'
      }).then(res => {
        this.list = res.data.list
        console.log(this.list);
      })
  },
  computed:{
    allnum(){
        return this.list.reduce((num,lis)=>{
              if(lis.goods_state){
                num += lis.goods_count
              }
              return num
        },0)
    },
    allPrice(){
      return this.list.reduce((num,lis)=>{
              if(lis.goods_state){
                num += lis.goods_price * lis.goods_count
              }
              return num
        },0)
    },
    
  }

}
</script>

<style lang="less" scoped>
.main{
    padding-top: 45px;
    padding-bottom: 50px;
}
</style>

头部组件

<template>
    <div class="my-header" :style="{color:color,background:bgcolor}">{{ tit }}</div>
  </template>
  
  <script>
  export default {
        props: ["color","bgcolor","tit"]
  }
  </script>
  
  <style lang="less" scoped>
    .my-header {
      height: 45px;
      line-height: 45px;
      text-align: center;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      z-index: 2;
    }
  </style>

内容组件

<template>
    <div class="my-goods-item" >
        <div class="left">
            <input type="checkbox" class="inp" name="check" v-model="lists.goods_state" :id="lists.id">
            <label :for="lists.id">
                 <img :src=lists.goods_img alt="">
            </label>
           
        </div>
        <div class="right">
            <div class="top">{{lists.goods_name}}</div>
            <div class="bottom">
                <span class="price">¥{{ lists.goods_price }}</span>
                <span>
                    <MyCount :lists="lists"></MyCount>
                </span>
            </div>
        </div>
    </div>
</template>
  
<script>
import MyCount from './MyCount.vue';
export default {
    components: { MyCount },
    props: ['lists']
}


</script>
  
<style lang="less" scoped>
.my-goods-item {
    display: flex;
    padding: 10px;
    border-bottom: 1px solid #ccc;

    .left {
        display: flex;
        .inp{
            margin-right:10px ;
        }
        img {
            width: 120px;
            height: 120px;
            margin-right: 8px;
            border-radius: 10px;
        }
        
    }

    .right {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .top {
            font-size: 14px;
            font-weight: 700;
        }

        .bottom {
            display: flex;
            justify-content: space-between;
            padding: 5px 0;
            align-items: center;

            .price {
                color: red;
                font-weight: bold;
            }
        }
    }
}</style>

控制数量的组件

<template>
    <div class="my-counter">
      <button type="button" class="btn" @click="sub" :disabled="lists.goods_count==1">-</button>
      <input type="number" class="inp" v-model="lists.goods_count">
      <button type="button" class="btn" @click="add">+</button>
    </div>
  </template>
  
  <script>
  export default {
      props: ["lists"],
      methods: {
          add(){
            this.lists.goods_count++;
          },
          sub(){
            this.lists.goods_count--;
          }
      },
      watch: {
        lists:{
          handler(){
            if(this.lists.goods_count < 1){
                this.lists.goods_count = 1;
            }
          },
          deep:true
        
        }
      }
  }
  </script>
  



  <style lang="less" scoped>
  .my-counter {
    display: flex;
    .inp {
      width: 30px;
      text-align: center;
      margin: 0 6px;
    }
    .btn{
      width: 20px;
    }
  }
  </style>

底部组件

<template>
    <!-- 底部 -->
    <div class="my-footer">
      <!-- 全选 -->
      <div >
        <input type="checkbox" v-model="isSelect">
        <label for="footerCheck" >全选</label>
      </div>
      <!-- 合计 -->
      <div>
        <span>合计:</span>
        <span class="price">¥ {{price}}</span>
      </div>
      <!-- 按钮 -->
      <button type="button" class="footer-btn">结算 ( {{num}} )</button>
    </div>
  </template>
  
  <script>
  export default {
      props: ["num","price","lists"],
      computed: {
        isSelect: {
            set(value) {
                this.lists.forEach(item => item.goods_state = value)
            },
            get(){
                return this.lists.every(item => item.goods_state)
            }
                
        }
      }
  }
  </script>
  
  <style lang="less" scoped>
  .my-footer {
    position: fixed;
    z-index: 2;
    bottom: 0;
    width: 100%;
    height: 50px;
    border-top: 1px solid #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    background: #fff;
    box-sizing: border-box;
    .price {
      color: red;
      font-weight: bold;
      font-size: 15px;
    }
    .footer-btn {
      min-width: 80px;
      height: 30px;
      line-height: 30px;
      border-radius: 25px;
      padding: 0;
      border: none;
      background: rgb(81, 142, 255);
      color: white;
    }
  }
  </style>

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于 Vue 2 实现简单购物车的示例代码: HTML: ``` <div id="app"> <h2>商品列表</h2> <ul> <li v-for="item in goods" :key="item.id"> {{ item.name }} - {{ item.price }}元 <button @click="addToCart(item)">加入购物车</button> </li> </ul> <h2>购物车</h2> <table> <thead> <tr> <th>名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item, index) in cart" :key="item.id"> <td>{{ item.name }}</td> <td>{{ item.price }}元</td> <td> <button @click="minusFromCart(index)" :disabled="item.quantity === 1">-</button> {{ item.quantity }} <button @click="addToCart(item)">+</button> </td> <td>{{ item.price * item.quantity }}元</td> <td> <button @click="removeFromCart(index)">删除</button> </td> </tr> </tbody> </table> <p>总价:{{ totalPrice }}元</p> </div> ``` JavaScript: ``` new Vue({ el: '#app', data: { goods: [ { id: 1, name: '商品1', price: 10 }, { id: 2, name: '商品2', price: 20 }, { id: 3, name: '商品3', price: 30 } ], cart: [] }, methods: { addToCart(item) { let index = this.cart.findIndex(cartItem => cartItem.id === item.id) if (index !== -1) { this.cart[index].quantity++ } else { this.cart.push({ id: item.id, name: item.name, price: item.price, quantity: 1 }) } }, minusFromCart(index) { if (this.cart[index].quantity > 1) { this.cart[index].quantity-- } }, removeFromCart(index) { this.cart.splice(index, 1) } }, computed: { totalPrice() { return this.cart.reduce((total, item) => total + item.price * item.quantity, 0) } } }) ``` 在这个示例中,首先定义了一个商品列表和一个购物车列表,其中商品列表包含了每个商品的名称、价格和 ID,购物车列表为空。在页面上展示商品列表时,为每个商品添加了一个加入购物车的按钮,并在点击按钮时调用 `addToCart` 方法将对应商品信息加入购物车列表中。在页面上展示购物车列表时,使用了一个表格来展示每个商品的名称、价格、数量和小计金额,并为每个商品添加了加减和删除按钮,同时使用了一个计算属性来计算购物车中所有商品的总价。在 `addToCart` 方法中还实现了判断购物车中是否已存在该商品的逻辑,如果存在则将对应商品的数量加 1,否则将该商品添加到购物车列表中。在 `minusFromCart` 方法中实现了减少购物车中商品数量的逻辑,并且如果商品数量已经为 1,则减少按钮会被禁用。在 `removeFromCart` 方法中通过 `splice` 方法实现了删除购物车列表中的商品的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值