最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vuex實(shí)現(xiàn)購(gòu)物車(chē)的增加減少移除

 更新時(shí)間:2020年06月28日 10:44:47   作者:緣飛夢(mèng)  
這篇文章主要為大家詳細(xì)介紹了vuex實(shí)現(xiàn)購(gòu)物車(chē)的增加減少移除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vuex實(shí)現(xiàn)購(gòu)物車(chē)增加減少移除的具體代碼,供大家參考,具體內(nèi)容如下

1.store.js(公共的倉(cāng)庫(kù))

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
 
export default new Vuex.Store({
 state: {
  carList: [] //購(gòu)物車(chē)的商品
 },
 mutations: {
  // 加
  addCar(state, params) {
   let CarCon = state.carList;
   // 判斷如果購(gòu)物車(chē)是否有這個(gè)商品,有就只增加數(shù)量,否則就添加一個(gè)
   // some 只要有一個(gè)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;
    }
   }
  },
 
   // 初始化購(gòu)物車(chē),有可能用戶(hù)一登錄直接進(jìn)入購(gòu)物車(chē)
  // initCar(state, car) {
  //  state.carList = car
  // },
 
 },
 actions: {
  // 加
  addCar({ commit }, params) {
   // console.log(params) //點(diǎn)擊添加傳過(guò)來(lái)的參數(shù)
   // 使用setTimeout模擬異步獲取購(gòu)物車(chē)的數(shù)據(jù)
   setTimeout(function () {
    let result = 'ok'
    if (result == 'ok') {
     // 提交給mutations
     commit("addCar", params)
    }
   }, 100)
  },
  // 減
  reducedCar({ commit }, params) {
   // console.log(params) //點(diǎn)擊添加傳過(guò)來(lái)的參數(shù)
   // 使用setTimeout模擬異步獲取購(gòu)物車(chē)的數(shù)據(jù)
   setTimeout(function () {
    let result = 'ok'
    if (result == 'ok') {
     // 提交給mutations
     commit("reducedCar", params)
    }
   }, 100)
  },
  // 移出
  deleteCar({ commit }, params) {
   // console.log(params) //點(diǎn)擊添加傳過(guò)來(lái)的參數(shù)
   // 使用setTimeout模擬異步獲取購(gòu)物車(chē)的數(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": '我是購(gòu)物車(chē)原來(lái)的',
  //     "price": 30,
  //     "num": 100,
  //    }])
  //   }
  //  }, 100)
  // }
 },
 getters: {
  //返回購(gòu)物車(chē)的總價(jià)
  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;
   }
  },
  //返回購(gòu)物車(chē)的總數(shù)
  carCount(state) {
   return state.carList.length
  }
 },
})

2. list.vue(商品列表)

 <template>
 <!-- 商品列表 -->
 <div id="listBox">
 <!-- -->
 <router-link :to="{path:'/car'}" style="line-height:50px">跳轉(zhuǎn)到購(gòu)物車(chē)</router-link>
 <el-table :data="tableData" border style="width: 100%">
  <el-table-column fixed prop="id" align="center" label="商品id"></el-table-column>
  <el-table-column prop="title" align="center" label="商品標(biāo)題"></el-table-column>
  <el-table-column prop="price" align="center" label="商品價(jià)格"></el-table-column>
  <el-table-column label="操作" align="center">
  <template slot-scope="scope">
   <el-button @click="addCar(scope.row)" type="text" size="small">加入購(gòu)物車(chē)</el-button>
  </template>
  </el-table-column>
 </el-table>
 </div>
</template>
 
<script>
export default {
 name: "listBox",
 data() {
 return {
  tableData: [] //商品列表
 };
 },
 methods: {
 // 初始化商品列表
 initTable(){
  this.$gAjax(`../static/shopList.json`)
  .then(res => {
   console.log(res)
   this.tableData=res;
  })["catch"](() => {});
 },
 // 加入購(gòu)物車(chē)
 addCar(row){
  // console.log(row)
  // 提交給store里面actions 由于加入購(gòu)物車(chē)的數(shù)據(jù)要同步到后臺(tái)
  this.$store.dispatch('addCar',row)
 }
 
 },
 mounted () {
 this.initTable()
 }
};
</script>
<style>
#listBox {
 width: 900px;
 margin: 0 auto;
}
</style>

3. car.vue(購(gòu)物車(chē))

<template>
 <!-- 購(gòu)物車(chē) -->
 <div id="carBox">
 <!-- 商品總數(shù) -->
 <h2 style="line-height:50px;font-size:16px;font-weight:bold">合計(jì):總共{{count}}個(gè)商品,總價(jià){{totalPrice}}元</h2>
 <p v-if="count==0">空空如也!·······</p>
 <div v-else>
  <el-table :data="carData" border style="width: 100%">
  <el-table-column fixed prop="id" align="center" label="商品id"></el-table-column>
  <el-table-column prop="title" align="center" label="商品標(biāo)題"></el-table-column>
  <el-table-column prop="price" align="center" label="商品價(jià)格"></el-table-column>
  <el-table-column label="操作" align="center">
   <template slot-scope="scope">
   <el-button @click="reduceFun(scope.row)" type="text" size="small">-</el-button>
   <span >{{scope.row.num}}</span>
   <el-button @click="addCar(scope.row)" type="text" size="small">+</el-button>
 
   <el-button @click="deleteFun(scope.row)" type="text" size="small">刪除</el-button>
   </template>
  </el-table-column>
  </el-table>
 </div>
 </div>
</template>
 
<script>
export default {
 name: "carBox",
 data() {
 return {};
 },
 computed: {
 //購(gòu)物車(chē)列表
 carData() {
  return this.$store.state.carList;
 },
 //商品總數(shù)
 count() {
  return this.$store.getters.carCount;
 },
 //商品總價(jià)
 totalPrice() {
  return this.$store.getters.totalPrice;
 }
 },
 methods: {
 // 增加數(shù)量
 addCar(row){
  this.$store.dispatch('addCar',row)
 },
 // 減數(shù)量
 reduceFun(row){
  this.$store.dispatch('reducedCar',row)
 },
 // 刪除
 deleteFun(row){
  this.$store.dispatch('deleteCar',row)
 }
 
 // 用戶(hù)首次登錄請(qǐng)求購(gòu)物車(chē)的數(shù)據(jù)
 // initCar(){
 // this.$store.dispatch('initCar')
 // }
 },
 created () {
 // this.initCar();
 },
 mounted() {}
};
</script>
 
<style>
#carBox {
 width: 900px;
 margin: 0 auto;
}
</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

大足县| 潮安县| 威远县| 林甸县| 广东省| 濮阳县| 习水县| 澳门| 沁阳市| 石泉县| 盈江县| 隆子县| 曲靖市| 龙南县| 芮城县| 永昌县| 方城县| 瑞安市| 民勤县| 昌宁县| 勃利县| 保山市| 奈曼旗| 凤山县| 白玉县| 吕梁市| 阆中市| 娱乐| 遵义市| 津南区| 永丰县| 江达县| 阿拉善左旗| 广西| 固始县| 楚雄市| 唐山市| 汾西县| 黄浦区| 泰顺县| 庆元县|