前端Vue学习之购物车项目实战记录

 更新时间:2024年07月29日 10:08:55   作者:坚持不懈的大白  
购物车是电商必备的功能,可以让用户一次性购买多个商品,下面这篇文章主要给大家介绍了关于前端Vue学习之购物车项目的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

1. json-server,生成后端接口

全局安装json-server,json-server官网为:json-server

npm install json-server -g
// 全局安装

安装之后启动可能存在json-server与node版本不兼容导致的问题,为此,建议指定一个json-sever版本。
需要准备一个json文件,然后在json文件中写入json数据,利用json-server,就可以实现增删改查功能。

{
    "books":[
        {"id":1,"bookName":"三国演义","price":23},            
        {"id":2,"bookName":"西游记","price":43},
        {"id":3,"bookName":"水浒传","price":33}
    ]
}

在这个json文件的目录下执行下述命令,

2. 购物车项目 - 实现效果

就是更改对应书本的购买数量,下面显示共计多少本书,以及需要多少钱实时更新。界面上构建了两个组件,分别为单个书本组件和下面总计组件。状态控制使用vuex.store来进行管理。

3. 参考代码 - Vuex

使用模块化对这个界面需要用到store进行封装,命名为books.js,代码如下:

import axios from 'axios'

const state = {
    books2:[]
};
const mutations = {
    updateBooks(state,newBooks){
        state.books2 = newBooks;
    },
    updateCount(state,obj){
        const book = state.books2.find(item => item.id == obj.id);
        book.count = obj.newCount;
    }
};
const actions = {
    async getBooks(context){
        const res = await axios.get('http://localhost:3000/books');
        context.commit('updateBooks',res.data);
    },
    async updateBooks(context,obj){
        await axios.patch(`http://localhost:3000/books/${obj.id}`,{
            count:obj.newCount
        })
        // 后台修改数据
        context.commit('updateCount',{
            id:obj.id,
            newCount:obj.newCount
        });
        // 前端页面显示
    }
};
const getters = {
    totalCount(state) {
        return state.books2.reduce((sum, item) => sum + item.count,0);
    },
    totalPrice(state) {
        return state.books2.reduce((sum, item) => sum + item.count * item.price,0);
    }
};

export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}

在store目录下index.js文件引入这个模块即可。

import books from './modules/books'

export default new Vuex.Store({
	...,
	modules:{
		books
	}
})

App.vue代码如下:

<template>
  <div id="app">
    <ul>
      <li v-for="item in books2" :key="item.id" class="sp">
        <Cart :item="item"></Cart>
      </li>
    </ul>
    <TotalPrice class="total-price-position"></TotalPrice>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import Cart from './components/Cart.vue';
import TotalPrice from './components/TotalPrice.vue';

export default {
  name: 'App',
  components: {
    Cart,TotalPrice
  },
  async created(){
    this.$store.dispatch('books/getBooks');
  },
  computed:{
    ...mapState('books',['books2'])
  }
}
</script>

<style lang="less" scoped>
  #app{
    position: relative;
    width: 100%;
    height: 700px;
    .total-price-position{
      position: absolute;
      bottom: 0;
      left: 0;
    }
  }
  .sp{
    height: 100px;
    margin-top: 5px;
    border-bottom: 1px solid yellow;
  }
</style>

当个书本组件代码如下:Cart.vue

<template>
    <div class="sp-item">
        <!-- <img :src="require('@/static/'+item.bookName+'.png')" alt=""> -->
        <img src="@/static/水浒传.png" alt="">
        <p class="sp-name">{{item.bookName}}</p>
        <p class="sp-price">¥{{item.price}}</p>
        <div class="sp-btn">
            <button class="sp-l-btn" @click="btnClick(-1)">-</button>
            <p class="sp-count">{{item.count}}</p>
            <button class="sp-r-btn" @click="btnClick(1)">+</button>
        </div>
    </div>
</template>

<script>

export default {
    name:'Cart',
    props:{
        item:Object
    },
    methods:{
        btnClick(step){
            const newCount = this.item.count + step;
            const id = this.item.id;

            if(newCount < 1)
                return
            this.$store.dispatch('books/updateBooks',{
                id,
                newCount
            })
        }
    }
}
</script>

<style lang="less" scoped>
    .sp-item{
        width: 100%;
        height: 100%;
        position: relative;
        >*{
            position: absolute;
        }
        img{
            width: 100px;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
        }
        .sp-name{
            top: 6px;
            left: 104px;
            font-size: 18px;
        }
        .sp-price{
            bottom: 4px;
            left: 104px;
            color: red;
            font-weight: 600;
        }
        .sp-btn{
            bottom: 4px;
            right: 2px;
            >*{
                display: inline-block;
                width: 20px;
                height: 20px;
                line-height: 20px;
                text-align: center;
            }
        }
    }

</style>

总计组件代码如下:TotalPrice.vue

<template>
    <div class="total-price-div">
        <span class="z-span"></span>
        共<span>{{totalCount}}</span>本,总共<span class="total-price">{{totalPrice}}</span>元
        <button>结算</button>
    </div>
</template>

<script>
import {mapGetters} from 'vuex';

export default {
    name:"TotalPrice",
    computed:{
        ...mapGetters('books',['totalCount','totalPrice'])
    }
}
</script>

<style scoped lang="less">
    .total-price-div{
        height: 60px;
        width: 100%;
        line-height: 60px;
        padding: 2px;
        background-color: #e1dcdc;
    }
    .total-price{
        color: red;
    }
    .z-span{
        width: 146px;
        display: inline-block;
    }
    button{
        color: white;
        background-color: green;
        border-radius: 6px;
        width: 60px;
        height: 40px;
        line-height: 40px;
    }
</style>

项目中需要用到axios、less、vuex。

总结

到此这篇关于前端Vue学习之购物车项目的文章就介绍到这了,更多相关前端Vue购物车项目内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue3中defineEmits与defineProps的用法实例

    vue3中defineEmits与defineProps的用法实例

    这篇文章主要介绍了vue3中defineEmits/defineProps的用法实例,需要的朋友可以参考下
    2023-12-12
  • 如何解决前端上传Formdata中的file为[object Object]的问题

    如何解决前端上传Formdata中的file为[object Object]的问题

    文件上传是Web开发中常见的功能之一,下面这篇文章主要给大家介绍了关于如何解决前端上传Formdata中的file为[object Object]问题的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-07-07
  • vue.js根据图片url进行图片下载

    vue.js根据图片url进行图片下载

    最近在做一个前端vue.js对接的功能模块时,需要实现一个下载图片的功能,本文就介绍了vue.js根据图片url进行图片下载,感兴趣的可以了解一下
    2021-06-06
  • Vue常用实例方法示例梳理分析

    Vue常用实例方法示例梳理分析

    在了解vue的常用的实例方法之前,我们应该先要了解其常用的实例属性,你能了解到的vue实例属性有哪些呢?小编在这里就列举了几个常用的vue实例的属性。大家可以一起参考学习一下
    2022-08-08
  • vue移动端项目缓存问题实践记录

    vue移动端项目缓存问题实践记录

    最近在做一个vue移动端项目,被缓存问题搞得头都大了,积累了一些经验,特此记录总结下,分享到脚本之家平台,对vue移动端项目缓存问题实践记录感兴趣的朋友跟随小编一起看看吧
    2018-10-10
  • 对vue事件的延迟执行实例讲解

    对vue事件的延迟执行实例讲解

    今天小编就为大家分享一篇对vue事件的延迟执行实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Vue核心概念Getter的使用方法

    Vue核心概念Getter的使用方法

    今天小编就为大家分享一篇关于Vue核心概念Getter的使用方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • 前端Vue.js实现json数据导出到doc

    前端Vue.js实现json数据导出到doc

    这篇文章主要介绍了前端Vue.js实现json数据导出到doc,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Vue组件之Tooltip的示例代码

    Vue组件之Tooltip的示例代码

    这篇文章主要介绍了Vue组件之Tooltip的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • vue单页面应用打开新窗口显示跳转页面的实例

    vue单页面应用打开新窗口显示跳转页面的实例

    今天小编就为大家分享一篇vue单页面应用打开新窗口显示跳转页面的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09

最新评论