欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端Vue學(xué)習之購物車項目實戰(zhàn)記錄

 更新時間:2024年07月29日 10:08:55   作者:堅持不懈的大白  
購物車是電商必備的功能,可以讓用戶一次性購買多個商品,下面這篇文章主要給大家介紹了關(guān)于前端Vue學(xué)習之購物車項目的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

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

全局安裝json-server,json-server官網(wǎng)為:json-server

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

安裝之后啟動可能存在json-server與node版本不兼容導(dǎo)致的問題,為此,建議指定一個json-sever版本。
需要準備一個json文件,然后在json文件中寫入json數(shù)據(jù),利用json-server,就可以實現(xiàn)增刪改查功能。

{
    "books":[
        {"id":1,"bookName":"三國演義","price":23},            
        {"id":2,"bookName":"西游記","price":43},
        {"id":3,"bookName":"水滸傳","price":33}
    ]
}

在這個json文件的目錄下執(zhí)行下述命令,

2. 購物車項目 - 實現(xiàn)效果

就是更改對應(yīng)書本的購買數(shù)量,下面顯示共計多少本書,以及需要多少錢實時更新。界面上構(gòu)建了兩個組件,分別為單個書本組件和下面總計組件。狀態(tài)控制使用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
        })
        // 后臺修改數(shù)據(jù)
        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>結(jié)算</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。

總結(jié)

到此這篇關(guān)于前端Vue學(xué)習之購物車項目的文章就介紹到這了,更多相關(guān)前端Vue購物車項目內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vuex中使用對象展開運算符的示例

    vuex中使用對象展開運算符的示例

    本篇文章主要介紹了vuex中使用對象展開運算符的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue如何使用CSS自定義變量

    Vue如何使用CSS自定義變量

    這篇文章主要介紹了Vue如何使用CSS自定義變量,幫助大家更好的理解和學(xué)習使用vue框架,感興趣的朋友可以了解下
    2021-05-05
  • 詳解Vue.js中的組件傳值機制

    詳解Vue.js中的組件傳值機制

    Vue.js 是一款流行的前端框架,它提供了一些方便的機制來管理組件之間的通信,其中包括組件傳值,本文將詳細介紹 Vue.js 中的組件傳值機制,包括父子組件傳值、兄弟組件傳值、跨級組件傳值等多種方式,需要的朋友可以參考下
    2023-08-08
  • 深入了解Vue3 中 this的使用

    深入了解Vue3 中 this的使用

    在Vue3中,this的使用方式與Vue2存在較大差異,尤其是在引入組合式API后,本文詳細解析了Vue3中this的使用情況、底層源碼和設(shè)計理念,并提供了面試技巧,感興趣的可以了解一下
    2024-09-09
  • vue實現(xiàn)簡單的星級評分組件源碼

    vue實現(xiàn)簡單的星級評分組件源碼

    這篇文章主要介紹了vue星級評分組件源碼,代碼簡單易懂非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Vue2.0仿餓了么webapp單頁面應(yīng)用詳細步驟

    Vue2.0仿餓了么webapp單頁面應(yīng)用詳細步驟

    本篇文章給大家分享了Vue2.0仿餓了么webapp單頁面應(yīng)用詳細步驟,有興趣的朋友可以跟著操作下。
    2018-07-07
  • vue?懸浮窗且?guī)ё詣游焦δ軐崿F(xiàn)demo

    vue?懸浮窗且?guī)ё詣游焦δ軐崿F(xiàn)demo

    這篇文章主要為大家介紹了vue?懸浮窗且?guī)ё詣游焦δ軐崿F(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 基于Vue+ECharts實現(xiàn)地圖展示與交互

    基于Vue+ECharts實現(xiàn)地圖展示與交互

    這篇文章中,我將逐步介紹如何使用 Vue 和 ECharts 實現(xiàn)一個互動式的地圖展示組件,其中支持返回上一層地圖、點擊查看不同城市的詳細信息,以及根據(jù)數(shù)據(jù)動態(tài)展示不同的統(tǒng)計信息,感興趣的小伙伴跟著小編一起來看看吧
    2025-02-02
  • vue.js實現(xiàn)點擊后動態(tài)添加class及刪除同級class的實現(xiàn)代碼

    vue.js實現(xiàn)點擊后動態(tài)添加class及刪除同級class的實現(xiàn)代碼

    這篇文章主要介紹了vue.js實現(xiàn)點擊后動態(tài)添加class及刪除同級class的相關(guān)資料,需要的朋友可以參考下
    2018-04-04
  • Vue單頁及多頁應(yīng)用全局配置404頁面實踐記錄

    Vue單頁及多頁應(yīng)用全局配置404頁面實踐記錄

    無論單頁還是多頁,我的實現(xiàn)思路是總體配置404頁面的思路就是在前端路由表中添加一個 path: '/404' 的路由,渲染相應(yīng)的404頁面。這篇文章主要介紹了Vue單頁及多頁應(yīng)用全局配置404頁面實踐,需要的朋友可以參考下
    2018-05-05

最新評論