商品加入購物車,保存在store中。大概一下步驟:
1.實現點擊加入購物車功能;
2.徽標數值的自動更新;
3.購物車商品的本地持久存儲;
4.獲取購物車商品並加載顯示;
5.加載顯示購物車列表時初始化numbox數量值;
6.實現購物車商品數量改變同步到store中;
7.購物車中商品的刪除;
8.把store中商品的選中狀態selected同步到頁面上;
9.同步shopcar頁面商品選中狀態到store中保存;
10.勾選商品的總價和數量自動計算
一、將商品加入保存到store中
<mt-button type="danger" size="small" @click="addToShopCar">加入購物車</mt-button>
拼接出一個對象,通過傳參的形式,傳給mutations中的方法,其中 numBoxCount 為 numbox 的值,即加入購物車的商品數,通過子組件調用父組件的方法,將值傳給父組件得來;
1 // 添加至購物車 2 addToShopCar(){ 3 this.ballFlag = !this.ballFlag; 4 5 // 拼接出一個商品對象保存到store中、{id:商品id,count:要購買的數量,price:商品價格,selected:false} 6 const YHHSelectShop = { 7 id:this.id, 8 count:this.numBoxCount, 9 price:this.youHaoHuo.price, 10 selected:true 11 }; 12 this.$store.commit('YHHSelectShopAddToShopCar',YHHSelectShop) 13 } 14 }
在mutations中,將商品存入到 localStorage中持久化,
1 YHHSelectShopAddToShopCar(state,YHHSelectShop) { 2 3 // 假設購物車中沒有找到 4 var flag = false; 5 6 state.shopCar.some(item => { 7 if (item.id == YHHSelectShop.id) { 8 item.count += parseInt(YHHSelectShop.count); 9 flag = true; 10 return true 11 } 12 }); 13 // 最終沒有找到,就把商品加入購物車中 14 if (!flag) { 15 state.shopCar.push(YHHSelectShop) 16 } 17 // 存儲到 localStorage 18 localStorage.setItem('shopCar',JSON.stringify(state.shopCar)) 19 },
然后store再從localStorage中取數據 const shopCarItem = JSON.parse(localStorage.getItem('shopCar') || '[]'); shopCar: shopCarItem
二、得到徽標數值,getters 中
// 徽標數值 getAllSelectShopCount(state){ var allCount = 0; state.shopCar.forEach(item => { allCount += item.count }); return allCount },
然后去APP.vue 顯示徽標數值
<router-link class="mui-tab-item-my" to="/shopcar">
<span class="mui-icon mui-icon-extra mui-icon-extra-cart">
<span class="mui-badge" id="shopCar">{{ $store.getters.getAllSelectShopCount }}</span></span>
<span class="mui-tab-label">購物車</span>
</router-link>
三、獲取購物車中所有商品列表並加載顯示,遍歷shopcar中的每件商品得到id,請求數據
1 // 獲取到 store 中所有的商品 2 async getShopCarList(){ 3 var idArr = []; 4 var shopId; 5 6 // 從store中拿到購物車中商品的id 7 this.$store.state.shopCar.forEach(item => idArr.push(item.id)); 8 9 // 如果購物車中沒有商品,直接返回 10 if (idArr.length === 0) { 11 return 12 } 13 // console.log(idArr) 14 // 循環得到id,分別請求數據 15 for (let i = 0;i < idArr.length;i++){ 16 17 shopId = parseInt(idArr[i]); 18 // console.log(typeof (shopId)) 19 20 if (shopId >= 1 && shopId <= 40) { 21 const res = await this.$axios.get('/getshoplistshopcar/'+shopId,this.model); 22 // console.log(res) 23 if (res.status === 200) { 24 this.shopCarList = this.shopCarList.concat(res.data) 25 } 26 } else if (shopId >= 41 && shopId <= 46) { 27 const res = await this.$axios.get('/getyouhaohuoshopcar/'+shopId,this.model); 28 // console.log(res) 29 if (res.status === 200) { 30 this.shopCarList = this.shopCarList.concat(res.data) 31 } 32 } else if (shopId >= 50 && shopId <= 139) { 33 const res = await this.$axios.get('/getyouhaohuoinfoshopcar/'+shopId,this.model); 34 // console.log(res) 35 if (res.status === 200) { 36 this.shopCarList = this.shopCarList.concat(res.data) 37 } 38 } else if (shopId >= 140 && shopId <= 229) { 39 const res = await this.$axios.get('/getaiguangjieinfoshopcar/'+shopId,this.model); 40 // console.log(res) 41 if (res.status === 200) { 42 this.shopCarList = this.shopCarList.concat(res.data) 43 } 44 } else if (shopId >= 230 && shopId <= 259) { 45 const res = await this.$axios.get('/getcainilikeshopcar/'+shopId,this.model); 46 // console.log(res) 47 if (res.status === 200) { 48 this.shopCarList = this.shopCarList.concat(res.data) 49 } 50 } 51 } 52 // console.log(this.shopCarList) 53 },
四、shopcar頁面numbox的加載顯示,getters 中
1 // shopcar 中各個商品的 numbox 值 2 getShopCarSelectCount(state){ 3 var shopCarCount = {}; 4 state.shopCar.forEach(item => { 5 // 商品的id作為鍵,數量作為值 6 shopCarCount[item.id] = item.count 7 }); 8 return shopCarCount 9 },
然后在shopcar頁面,將值傳給numbox子組件,子組件通過props接受Count值,
<NumBox
:Count = "$store.getters.getShopCarSelectCount[item.id]" // 商品件數轉給子組件
:shopId = "item.id" // 商品id傳給子組件
></NumBox>
五、shopcar頁面numbox改變同步到store中
mutations中
1 // shopcar 頁面修改 numbox 2 updateShopCarCount(state,selectShop) { 3 state.shopCar.some(item => { 4 if (item.id == selectShop.id) { // item.id 類型為Number,selectShop.id 類型為string 5 item.count = parseInt(selectShop.count); 6 return true 7 } 8 }); 9 // 當修改完商品的數量,把最新的數量保存到本地存儲中 10 localStorage.setItem('shopCar',JSON.stringify(state.shopCar)) 11 }
shopcar-numbox組件中,調用mutations中的方法,將值傳給mutations
1 countChanged(){ // 數量改變 2 // console.log(this.$refs.numBox.value) 3 // 每當數量值改變,立即把當前最新的數量同步到 store 中,覆蓋之前的數量值 4 this.$store.commit("updateShopCarCount",{ 5 id:this.shopId, 6 count:this.$refs.numBox.value 7 }) 8 }
六、商品的刪除
刪兩個部分,1.界面的商品數組中;2.store中
shopcar組件中
1 // 取消訂單 2 remove(id,index){ 3 // 點擊刪除,把商品從 store 中根據傳遞的id刪除 4 // 同時把當前組件中的 shopCarList 中,對應要刪除的那個商品使用index來刪除 5 this.shopCarList.splice(index,1); // 刪數組shopCarList中 6 7 this.$store.commit('removeFromShopCar',id) // 刪store中 8 }
mutations中
1 // 根據id從store中的購物車中刪除對應的數據 2 removeFromShopCar(state,id) { 3 state.shopCar.some((item,i) => { 4 if (item.id == id) { 5 state.shopCar.splice(i,1); 6 return true 7 } 8 }); 9 localStorage.setItem('shopCar',JSON.stringify(state.shopCar)) 10 }
七、store 中商品選中的狀態 selected 同步到頁面上
getters 中
1 // 商品選中狀態 2 getShopCarSelected(state) { 3 var Obj = {}; 4 state.shopCar.forEach(item => { 5 Obj[item.id] = item.selected 6 }); 7 return Obj 8 },
在shopcar頁面switch中
<mt-switch class="shopCar-switch" v-model="$store.getters.getShopCarSelected[item.id]"> </mt-switch>
八、selected 狀態改變同步到store中
mutations 中
1 // 改變商品選中 2 updateShopCarSelected(state,switchInfo) { 3 state.shopCar.some(item => { 4 if (item.id == switchInfo.id) { 5 item.selected = switchInfo.selected 6 } 7 }); 8 localStorage.setItem('shopCar',JSON.stringify(state.shopCar)) 9 }
shopcar中,將id及狀態傳給mutations
<mt-switch class="shopCar-switch" v-model="$store.getters.getShopCarSelected[item.id]" @change="selectedChange(item.id,$store.getters.getShopCarSelected[item.id])"> </mt-switch>
1 // 改變商品選中 2 selectedChange(id,newVal) { 3 console.log(id+'---'+newVal) 4 this.$store.commit('updateShopCarSelected',{ id , selected : newVal }) 5 }
九、勾選數量和總價的計算
getters中
1 // 得到 總價 和 總量 2 getShopCarAllPriceAndCount(state) { 3 var Obj = { 4 COUNT:0, // 勾選的數量 5 PRICE:0 // 勾選的總價 6 }; 7 state.shopCar.forEach(item => { 8 if (item.selected) { // 被勾選 9 Obj.COUNT += item.count; 10 Obj.PRICE += item.price * item.count 11 } 12 }); 13 return Obj 14 }
shopcar頁面
<p> 已選 <span class="red">{{ $store.getters.getShopCarAllPriceAndCount.COUNT }}</span> 件,總價 <span class="red">¥{{ $store.getters.getShopCarAllPriceAndCount.PRICE }}</span>(不含運費) </p>