溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vuex實(shí)現(xiàn)購(gòu)物車小功能的方法

發(fā)布時(shí)間:2020-08-19 10:53:30 來源:億速云 閱讀:209 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Vuex實(shí)現(xiàn)購(gòu)物車小功能的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

功能描述:

  • 加購(gòu)
  • 刪除
  • 加減
  • 全選反選
  • 選中計(jì)算總價(jià)
  • 存儲(chǔ)
     

整體演示效果如下:

Vuex實(shí)現(xiàn)購(gòu)物車小功能的方法

首先介紹一下Vuex:

Vuex 實(shí)例對(duì)象屬性 主要有下面5個(gè)核心屬性:

state : 全局訪問的state對(duì)象,存放要設(shè)置的初始狀態(tài)名及值(必須要有)

mutations : 里面可以存放改變 state 的初始值的方法 ( 同步操作–必須要有 )

getters: 實(shí)時(shí)監(jiān)聽state值的變化可對(duì)狀態(tài)進(jìn)行處理,返回一個(gè)新的狀態(tài),相當(dāng)于store的計(jì)算屬性(不是必須的)

actions : 里面可以存放用來異步觸發(fā) mutations 里面的方法的方法 ( 異步操作–不是必須的 )

modules : 存放模塊化的數(shù)據(jù)(不是必須的)

一、主頁(yè)面Home:

<template>
 <div id="app">
  <div class="nav-top">
   <!--  標(biāo)簽欄-->
   <van-nav-bar
    title="商品列表頁(yè)"
    left-arrow
   />
  </div>
  <div class="nav-bottom">
   <!--  商品卡片-->
   <van-card
    v-for="item in goodsList"
    :price="item.actualPrice"
    :desc="item.desc"
    :title="item.dtitle"
    :thumb="item.mainPic"
   >
   <template #num>
    <van-icon name="shopping-cart-o" color="red" size="24px" @click="add(item)"/>
   </template>
   </van-card>
  </div>
 </div>
</template>

<script>

 export default {
  data() {
   return {
   goodsList: [], // 商品列表數(shù)據(jù)
   }
  },
  // 請(qǐng)求商品列表數(shù)據(jù)
  mounted() {
  // 接口不予展示,有需要請(qǐng)自行下載
   this.$axios.get('api接口').then(res => {
   this.goodsList = res.data.data.data
   // console.log(this.goodsList)
   })
  },
  methods: {
   // 添加商品 調(diào)用vuex中的add方法
   add(item) {
   this.$store.commit('add', item)
   }
  }
 }
</script>

<style lang="scss" scoped>
 #app {
  .nav-top {
   width: 100%;
   position: fixed;
   top: 0;
   left: 0;
   z-index: 9;
  }
  .nav-bottom {
   margin-top: 50px;
  }
 }
</style>

二、購(gòu)物車頁(yè)面ShopCart:

<template>
 <div>
  <!--  標(biāo)簽欄-->
  <van-nav-bar
   title="購(gòu)物車"
   left-arrow
   @click-left="back"
  />
  <!--  購(gòu)物車box -->
  <div class="car-box" v-for="(item,index) in list" :key="index">
   <!-- 左側(cè)復(fù)選框布局-->
   <div class="car-box-left">
   <van-checkbox v-model="item.ckd"></van-checkbox>
   </div>
   <!-- 右側(cè)商品卡片布局-->
   <div class="car-box-right">
   <van-swipe-cell>
    <van-card
      :price="item.item.actualPrice"
      :title="item.item.dtitle"
      :thumb="item.item.mainPic"
    >
     <!-- 步進(jìn)器-->
     <template #num>
      <van-stepper v-model="item.num" theme="round" button-size="22" disable-input
         @change="changeNum(item.num)"/>
     </template>
    </van-card>
    <!--  右側(cè)滑動(dòng)刪除-->
    <template #right>
     <van-button square text="刪除" type="danger" class="delete-button" @click="del(index)"/>
    </template> 
   </van-swipe-cell>
   </div>
  </div>
  <!-- 空狀態(tài) 沒數(shù)據(jù)顯示 有數(shù)據(jù)隱藏-->
  <van-empty
   v-show="$store.state.cartList.length==0"
   class="custom-image"
   image="https://img.yzcdn.cn/vant/custom-empty-image.png"
   description="購(gòu)物車是空的喲!"
  />
  <!--  商品導(dǎo)航-->
  <van-submit-bar :price="$store.getters.total*100" button-text="提交訂單">
   <van-checkbox v-model="checkAll">全選</van-checkbox>
  </van-submit-bar>

 </div>
</template>

<script>
 import {mapMutations} from 'vuex'
 export default {
  data() {
   return {
   list: this.$store.state.cartList, //購(gòu)物車數(shù)據(jù)
   }
  },
  computed: {
   // 計(jì)算屬性checkAll 和全選按鈕雙向數(shù)據(jù)綁定,別人可以控制它 它也可以控制別人
   // 別人控制它 給他一個(gè)值的時(shí)候是 get , 它控制別人 給別人設(shè)置值的時(shí)候 是set
   // 在set中newVal參數(shù)是這個(gè)計(jì)算屬性改變后的值
   checkAll: { //可以看作一個(gè)事件
   get() {
    // 判斷購(gòu)物車?yán)锷唐返拈L(zhǎng)度為0 return false
    if (this.list.length == 0) {
     return false
    }
    return this.$store.state.cartList.every(item => {
     return item.ckd == true // 返回結(jié)果復(fù)選框?yàn)閠rue
    })
   },
   set(newVal) {
    this.$store.commit('ckd', newVal)
   },
   }
  },
  methods: {
   // 返回商品列表頁(yè)
   back() {
   this.$router.push({
    path: '/'
   })
   },
   //方法集合
   ...mapMutations(['del', 'changeNum',])
  },
 }
</script>

<style lang="scss" scoped>
 .goods-card {
  margin: 0;
  background-color: white;
 }
 .delete-button {
  height: 100%;
 }
 .car-box {
  width: 100%;
  margin-bottom: 5px;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  .car-box-left {
   flex: 1;
   padding-left: 10px;
  }
  .car-box-right {
   flex: 12;
  }
 }
</style>

三、Vuex代碼:

import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersist from 'vuex-persist'

Vue.use(Vuex);

export default new Vuex.Store({
 state: {
  cartList: [], // 購(gòu)物車數(shù)據(jù)
 },
 mutations: {
  // 添加商品
  add(state, item) {
   // console.log(item)
   let flag = false;
   // 加購(gòu)去重(通過id判斷)
   state.cartList.forEach(i => {
   if (i.item.id == item.id) {
    i.num++;
    flag = true;
   }
   })
   if (flag == false) {
   state.cartList.push({
    num: 1, // 添加數(shù)量默認(rèn)為1
    item, // 添加購(gòu)物車商品數(shù)據(jù)
    ckd: false, // 添加復(fù)選框初始化狀態(tài)為false
   })
   }
   // console.log(state.cartList)
  },
  // 刪除商品
  del(state, index) {
   state.cartList.splice(index, 1)
   // state.
  },
  // 改變數(shù)量------加減綜合法 ?。。?
  changeNum(state, index) {
   state.cartList.num = index
  },
  // 全選全不選
  ckd(state, newAll) {
   state.cartList.forEach(item => {
   item.ckd = newAll
   })
  }

 },
 // 計(jì)算 getters
 getters: {
  // 總價(jià)
  total(state) {
   let sum = 0;
   state.cartList.forEach(item => {
   // 如果復(fù)選框選中 計(jì)算總價(jià)
   if (item.ckd == true) {
    sum += item.item.actualPrice * item.num
   }
   })
   return sum
  }
 },
 actions: {},
 modules: {},
 // Vuex存儲(chǔ)插件
 plugins: [
  new VuexPersist({
   storage: window.localStorage,
  }).plugin,
 ]
})

看完了這篇文章,相信你對(duì)Vuex實(shí)現(xiàn)購(gòu)物車小功能的方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI