最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

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

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

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

就是更改對(duì)應(yīng)書本的購買數(shù)量,下面顯示共計(jì)多少本書,以及需要多少錢實(shí)時(shí)更新。界面上構(gòu)建了兩個(gè)組件,分別為單個(gè)書本組件和下面總計(jì)組件。狀態(tài)控制使用vuex.store來進(jìn)行管理。

3. 參考代碼 - Vuex

使用模塊化對(duì)這個(gè)界面需要用到store進(jìn)行封裝,命名為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
        })
        // 后臺(tái)修改數(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文件引入這個(gè)模塊即可。

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>

當(dāng)個(gè)書本組件代碼如下: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>

總計(jì)組件代碼如下: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>

項(xiàng)目中需要用到axios、less、vuex。

總結(jié)

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

相關(guān)文章

  • vue3中defineEmits與defineProps的用法實(shí)例

    vue3中defineEmits與defineProps的用法實(shí)例

    這篇文章主要介紹了vue3中defineEmits/defineProps的用法實(shí)例,需要的朋友可以參考下
    2023-12-12
  • 如何解決前端上傳Formdata中的file為[object?Object]的問題

    如何解決前端上傳Formdata中的file為[object?Object]的問題

    文件上傳是Web開發(fā)中常見的功能之一,下面這篇文章主要給大家介紹了關(guān)于如何解決前端上傳Formdata中的file為[object?Object]問題的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • vue.js根據(jù)圖片url進(jìn)行圖片下載

    vue.js根據(jù)圖片url進(jìn)行圖片下載

    最近在做一個(gè)前端vue.js對(duì)接的功能模塊時(shí),需要實(shí)現(xiàn)一個(gè)下載圖片的功能,本文就介紹了vue.js根據(jù)圖片url進(jìn)行圖片下載,感興趣的可以了解一下
    2021-06-06
  • Vue常用實(shí)例方法示例梳理分析

    Vue常用實(shí)例方法示例梳理分析

    在了解vue的常用的實(shí)例方法之前,我們應(yīng)該先要了解其常用的實(shí)例屬性,你能了解到的vue實(shí)例屬性有哪些呢?小編在這里就列舉了幾個(gè)常用的vue實(shí)例的屬性。大家可以一起參考學(xué)習(xí)一下
    2022-08-08
  • vue移動(dòng)端項(xiàng)目緩存問題實(shí)踐記錄

    vue移動(dòng)端項(xiàng)目緩存問題實(shí)踐記錄

    最近在做一個(gè)vue移動(dòng)端項(xiàng)目,被緩存問題搞得頭都大了,積累了一些經(jīng)驗(yàn),特此記錄總結(jié)下,分享到腳本之家平臺(tái),對(duì)vue移動(dòng)端項(xiàng)目緩存問題實(shí)踐記錄感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • 對(duì)vue事件的延遲執(zhí)行實(shí)例講解

    對(duì)vue事件的延遲執(zhí)行實(shí)例講解

    今天小編就為大家分享一篇對(duì)vue事件的延遲執(zhí)行實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Vue核心概念Getter的使用方法

    Vue核心概念Getter的使用方法

    今天小編就為大家分享一篇關(guān)于Vue核心概念Getter的使用方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 前端Vue.js實(shí)現(xiàn)json數(shù)據(jù)導(dǎo)出到doc

    前端Vue.js實(shí)現(xiàn)json數(shù)據(jù)導(dǎo)出到doc

    這篇文章主要介紹了前端Vue.js實(shí)現(xiàn)json數(shù)據(jù)導(dǎo)出到doc,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Vue組件之Tooltip的示例代碼

    Vue組件之Tooltip的示例代碼

    這篇文章主要介紹了Vue組件之Tooltip的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • vue單頁面應(yīng)用打開新窗口顯示跳轉(zhuǎn)頁面的實(shí)例

    vue單頁面應(yīng)用打開新窗口顯示跳轉(zhuǎn)頁面的實(shí)例

    今天小編就為大家分享一篇vue單頁面應(yīng)用打開新窗口顯示跳轉(zhuǎn)頁面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評(píng)論

平武县| 腾冲县| 炎陵县| 泰顺县| 东兰县| 和龙市| 衡南县| 徐州市| 尼木县| 新乐市| 图木舒克市| 乳山市| 邹城市| 木里| 双峰县| 交城县| 鱼台县| 崇阳县| 吕梁市| 望都县| 平原县| 突泉县| 大厂| 恩施市| 集贤县| 彰化市| 湄潭县| 巴林右旗| 玛纳斯县| 四平市| 林口县| 罗江县| 托里县| 会理县| 北海市| 五华县| 紫金县| 天台县| 海南省| 长顺县| 鲜城|