1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
const shop = { namespaced: true, state: { articles: [ { id: 1, name: "可乐", price: 3, count: 1, status: true }, { id: 2, name: "炸鸡", price: 12, count: 1, status: true }, { id: 3, name: "汉堡", price: 6, count: 1, status: false }, ], totalPrice: 0 }, mutations: { addCount: function (state, id) { state.articles[id - 1].count++; this.commit("shop/setTotalPrice"); }, decreaseCount: function (state, id) { if (state.articles[id - 1].count === 1) { console.log("商品数量已经无法减少"); } else { state.articles[id - 1].count-- } this.commit("shop/setTotalPrice"); }, setTotalPrice: function (state) { state.totalPrice = 0; for (let i = 0; i < this.getters["shop/getShopList"].length; i++) { let price = this.getters["shop/getShopList"][i].count * this.getters["shop/getShopList"][i].price; state.totalPrice = state.totalPrice + price } }
}, actions: { incrementAsync: function () { setTimeout(function () { console.log("异步操作") }, 2000) } }, getters: { getShopList: state => { return state.articles.filter(article => article.status) } } };
export default shop;
|