如何用vuex實現(xiàn)購物車的增加減少移除-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

我們擁有10多年網(wǎng)頁設(shè)計和網(wǎng)站建設(shè)經(jīng)驗,從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計師為您提供的解決方案。為企業(yè)提供做網(wǎng)站、成都網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、手機網(wǎng)站制作設(shè)計、H5頁面制作、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計或者設(shè)計方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計服務(wù)并滿足您的需求。

這篇文章主要介紹如何用vuex實現(xiàn)購物車的增加減少移除,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

1.store.js(公共的倉庫)

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
 
export default new Vuex.Store({
 state: {
  carList: [] //購物車的商品
 },
 mutations: {
  // 加
  addCar(state, params) {
   let CarCon = state.carList;
   // 判斷如果購物車是否有這個商品,有就只增加數(shù)量,否則就添加一個
   // some 只要有一個isHas為true,就為true
   let isHas = CarCon.some((item) => {
    if (params.id == item.id) {
     item.num++;
     return true;
    } else {
     return false;
    }
   })
 
   if (!isHas) {
    let obj = {
     "id": params.id,
     "title": params.title,
     "price": params.price,
     "num": 1,
    }
    this.state.carList.push(obj)
   }
  },
  // 減
  reducedCar(state,params){
   let len=state.carList.length;
   for(var i=0;i<len;i++){
    if(state.carList[i].id==params.id){
     state.carList[i].num--
     if(state.carList[i].num==0){
      state.carList.splice(i,1);
      break;
     }
    }
   }
  },
  //移出
  deleteCar(state,params){
   let len=state.carList.length;
   for(var i=0;i<len;i++){
    if(state.carList[i].id==params.id){
     state.carList.splice(i,1);
     break;
    }
   }
  },
 
   // 初始化購物車,有可能用戶一登錄直接進入購物車
  // initCar(state, car) {
  //  state.carList = car
  // },
 
 },
 actions: {
  // 加
  addCar({ commit }, params) {
   // console.log(params) //點擊添加傳過來的參數(shù)
   // 使用setTimeout模擬異步獲取購物車的數(shù)據(jù)
   setTimeout(function () {
    let result = 'ok'
    if (result == 'ok') {
     // 提交給mutations
     commit("addCar", params)
    }
   }, 100)
  },
  // 減
  reducedCar({ commit }, params) {
   // console.log(params) //點擊添加傳過來的參數(shù)
   // 使用setTimeout模擬異步獲取購物車的數(shù)據(jù)
   setTimeout(function () {
    let result = 'ok'
    if (result == 'ok') {
     // 提交給mutations
     commit("reducedCar", params)
    }
   }, 100)
  },
  // 移出
  deleteCar({ commit }, params) {
   // console.log(params) //點擊添加傳過來的參數(shù)
   // 使用setTimeout模擬異步獲取購物車的數(shù)據(jù)
   setTimeout(function () {
    let result = 'ok'
    if (result == 'ok') {
     // 提交給mutations
     commit("deleteCar", params)
    }
   }, 100)
  }
  // initCar({ commit }) {
  //  setTimeout(function () {
  //   let result = 'ok'
  //   if (result == 'ok') {
  //    // 提交給mutations
  //    commit("initCar", [{
  //     "id": 20193698,
  //     "title": '我是購物車原來的',
  //     "price": 30,
  //     "num": 100,
  //    }])
  //   }
  //  }, 100)
  // }
 },
 getters: {
  //返回購物車的總價
  totalPrice(state) {
   let Carlen = state.carList;
   let money = 0;
   if (Carlen.length != 0) {
    Carlen.forEach((item) => {
     money += item.price * item.num
    })
    return money;
   } else {
    return 0;
   }
  },
  //返回購物車的總數(shù)
  carCount(state) {
   return state.carList.length
  }
 },
})

網(wǎng)頁題目:如何用vuex實現(xiàn)購物車的增加減少移除-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://bm7419.com/article26/dpdsjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站Google、網(wǎng)站設(shè)計云服務(wù)器、建站公司網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化