您好,登錄后才能下訂單哦!
利用Vuex怎么實現一個購物車功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
文件結構
App.vue
<template> <div id="app"> <h4>Shopping Cart Demo</h4> <hr> <h5>Products:</h5> <ProductList /> <hr> <h5>My Cart</h5> <ShoppingCart /> </div> </template> <script> import ProductList from '@/components/ProductList'; import ShoppingCart from '@/components/ShoppingCart'; export default { components: { ProductList, ShoppingCart } } </script>
Products.vue
<template> <div> <ul v-for="item in products" :key="item.id"> <li> {{ item.title }} - {{ item.price }} 庫存: {{ item.inventory }}<br> <button :disabled="!item.inventory" @click="addToCart(item)">add to cart</button> </li> </ul> </div> </template> <script> import { mapGetters, mapActions } from 'vuex'; export default { computed: { // ...mapGetters('products',{ // products: 'allProducts' // }) ...mapGetters({ products: 'products/allProducts' }) }, methods: { ...mapActions('cart',['addToCart']) }, created() { this.$store.dispatch('products/getAllProducts'); } } </script>
ShoppingCart.vue
<template> <div> <ul v-for="item in products" :key="item.id"> <li>{{ item.title }} *{{ item.quantity }}</li> </ul> <div>total: {{ total }}</div> </div> </template> <script> import { mapGetters } from 'vuex'; export default { computed: { ...mapGetters('cart', { products: 'cartProducts', total: 'cartTotalPrice' }) } } </script>
modules/products.js
import api from '../../api'; const state = { all: [] } const getters = { allProducts: state => state.all } const actions = { // 獲取初始商品數據 getAllProducts({ commit }) { api.getProducts(products => commit('setProducts', products)); } } const mutations = { setProducts(state, products) { state.all = products; }, // 減少該商品庫存 decreamentInventory(state, { id }) { let productItem = state.all.find(item => item.id === id); productItem.inventory --; } } export default { namespaced: true, state, getters, actions, mutations }
modules/cart.js
const state = { addedList: [] } const getters = { cartProducts(state, getters, rootState) { return state.addedList.map((item, index) => { let productItem = rootState.products.all.find(product => product.id === item.id); return { title: productItem.title, price: productItem.price, quantity: item.quantity } }) }, cartTotalPrice(state, getters) { return getters.cartProducts.reduce((total, product) => { return total + (product.price * product.quantity); }, 0); } } const actions = { addToCart({ state, commit }, product) { if (product.inventory > 0) { let productItem = state.addedList.find(item => item.id === product.id); if (productItem) { commit('increamentItemQuantity', productItem); } else { commit('pushItemToCart', product); } commit('products/decreamentInventory', product, { root: true }); } } } const mutations = { // 增加購物車中同等商品的數量 increamentItemQuantity(state, { id }) { let productItem = state.addedList.find(item => item.id === id); productItem.quantity ++; }, // 將商品加入購物車 pushItemToCart(state, { id }) { state.addedList.push({ id, quantity: 1 }) }, } export default { namespaced: true, state, getters, actions, mutations }
store/index.js
import Vue from 'vue'; import Vuex from 'vuex'; import cart from './modules/cart'; import products from './modules/products'; Vue.use(Vuex); export default new Vuex.Store({ modules: { cart, products } });
main.js
import Vue from "vue"; import App from "@/components/App.vue"; import store from "@/store"; Vue.config.productionTip = false; new Vue({ store, render: h => h(App) }).$mount("#app");
關于利用Vuex怎么實現一個購物車功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。