国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

vuex實(shí)現簡(jiǎn)單的購物車(chē)功能

發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:UIEngineer 欄目: JavaScript 歡迎投稿:712375056

本文實(shí)例為大家分享了vuex實(shí)現購物車(chē)功能的具體代碼,供大家參考,具體內容如下

文件目錄如下:

購物車(chē)組件

<template>
    <div>
        <h1>vuex-shopCart</h1>
        <div class="shop-listbox">
            <shop-list />
        </div>
        <h2>已選商品</h2>
        <div class="shop-cartbox">
            <shop-cart />
        </div>
    </div>
</template>

<script>
import shoList from './shop-list'
import shopCart from './shop-cart'

export default {
  name: 'shop',
  components: {
      'shop-list' : shoList,
      'shop-cart' : shopCart
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

商品列表

<template>
    <div class="shop-list">
        <table>
            <tr class="shop-listtitle">
                <td>id</td>
                <td>名稱(chēng)</td>
                <td>價(jià)格</td>
                <td>操作</td>
            </tr>
            <tr v-for = "item in shopList" class="shop-listinfo" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button @click="addToCart(item)">加入購物車(chē)</button>
                </td>
            </tr>
        </table>
    </div>
</template>

<script>
import {mapGetters,mapActions} from "vuex";
export default {
    name : 'shopList',
    computed: {
        ...mapGetters({
                shopList:'getShopList',
            })
    },
    methods: {
        ...mapActions(['addToCart'])
    },
}
</script>

選中商品列表

<template>
    <div class="shop-list">
        <table>
            <tr class="shop-listtitle">
                <td>id</td>
                <td>名稱(chēng)</td>
                <td>價(jià)格</td>
                <td>數量</td>
                <td>操作</td>
            </tr>
            <tr v-for="item in cartData" class="shop-listinfo" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>{{item.num}}</td>
                <td><button class="shop-dele dele-btn" @click="deleteShop(item)">刪除</button></td>
            </tr>
            <tr v-if="cartData.length <= 0">
                <td colspan="5">暫無(wú)數據</td>
            </tr>
            <tr>
                <td colspan="2">總數:{{totalNum}}</td>
                <td colspan="2">總價(jià)格:{{totalPrice}}</td>
                <td><button class="dele-cart dele-btn" @click="clearCart">清空購物車(chē)</button></td>
            </tr>
        </table>
    </div>
</template>

<script>
import {mapGetters,mapActions} from 'vuex'
export default {
    name : 'shopCart',
    data(){
        return{
            
        }
    },
    computed: {
        ...mapGetters({
            cartData:'addShopList',
            totalNum : 'totalNum',
            totalPrice:'totalPrice'
        })
    },
    methods: {
        ...mapActions({
            clearCart:'clearToCart',
            deleteShop:"deletToShop"
        })
    }
}
</script>

vuex 創(chuàng )建

npm install vuex --save,創(chuàng )建vuex文件夾,在文件夾中創(chuàng )建store.js,引入vuex;

store.js

import Vue from "vue"
import Vuex from 'vuex'
import cart from './modules/cart'

Vue.use(Vuex)

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

建立一個(gè)模塊文件夾modules,里面創(chuàng )建創(chuàng )建當個(gè)store模塊,然后默認輸出,在store.js中引入;

cart.js

const state = {
    shop_list: [{
        id: 11,
        name: '魚(yú)香肉絲',
        price : 12
    }, {
            id: 22,
            name: '宮保雞丁',
            price : 14
        }, {
            id: 34,
            name: '土豆絲',
            price : 10
        }, {
            id: 47,
            name: '米飯',
            price : 2
        }, {
            id: 49,
            name: '螞蟻上數',
            price : 13
        }, {
            id: 50,
            name: '臘肉炒蒜薹',
            price : 15
        }],
        add : []
}

const getters = {
    // 獲取商品列表
    getShopList: state => state.shop_list,
    // 獲取購物車(chē)列表
    addShopList: state => {
        // map()方法返回一個(gè)新數組,數組中的元素為原始數組元素調用函數處理后的值
        return state.add.map(({ id, num }) => {
            let product = state.shop_list.find(n => n.id == id)// find()方法返回通過(guò)測試(函數內判斷)的數組的第一個(gè)元素的值,如果沒(méi)有符合條件的元素返回undefined
            if (product) {//    如果存在該商品
                return {//  返回對象
                    ...product,
                    num
                }
            }
        })
    },
     // 獲取總數量
     totalNum: (state, getters) => {
         let total = 0
         getters.addShopList.map(n => { 
             total += n.num
         })
         return total
    },
    // 計算總價(jià)格
    totalPrice: (state, getters) => { 
        let total = 0
        getters.addShopList.map(n => { 
            total += n.num * n.price
        })
        return total
    }
},

const actions = {
    // 加入購物車(chē)
    addToCart({ commit},product) { 
        commit('addCart', {
            id : product.id
        })
    },
    // 清空購物車(chē)
    clearToCart({ commit}) { 
        commit('clearCart')
    },
    // 刪除單個(gè)物品
    deletToShop({ commit},product) { 
        commit('deletShop',product)
    }
}

const mutations = {
    // 加入購物車(chē)
    addCart(state, { id}){ 
        let record = state.add.find(n => n.id == id)
        if (!record) {//   如果購物車(chē)中不存在該商品
            state.add.push({//  追加商品
                id,
                num : 1
            })
        } else { // 如果商品已經(jīng)加入購物車(chē),則改變數量
            record.num++
        }
    },
    // 刪除單個(gè)物品
    deletShop(state, product) { 
        state.add.forEach((item,i) => { 
            if (item.id == product.id) {//  如果找到該商品 
                state.add.splice(i,1)
            }
        })
    },
    // 清空購物車(chē)
    clearCart(state) { 
        state.add = []
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}

以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

亚洲AV伊人久久综合密臀性色| 亚洲AV无码精品色午夜APP| 国产乱子伦一区二区三区视频播放| 亚洲午夜理论无码电影| 免费无码专区毛片高潮喷水| 女同免费毛片在线播放|