欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vuex實現的簡單購物車功能示例

 更新時間:2019年02月13日 08:38:16   作者:東邊的小山  
這篇文章主要介紹了vuex實現的簡單購物車功能,結合實例形式分析了vuex購物車組件相關商品列表、購物車創(chuàng)建、添加、刪除、清空等相關操作技巧,需要的朋友可以參考下

本文實例講述了vuex實現的簡單購物車功能。分享給大家供大家參考,具體如下:

購物車組件

<template>
  <div>
    <h1>vuex-shopCart</h1>
    <div class="shop-listbox">
      <shop-list/>
    </div>
    <h2>已選商品</h2>
    <div class="shop-cartbox">
      <shop-cart/>
    </div>
  </div>
</template>
<script>
  import shopList from "./shop-list";
  import shopCart from './shop-cart';
  export default{
    name:'shop',
    components:{
      'shop-list':shopList,
      'shop-cart':shopCart
    }
  }
</script>

商品列表

<template>
  <div class="shop-list">
    <table>
      <tr class="shop-listtitle">
        <td>id</td>
        <td>名稱</td>
        <td>價格</td>
        <td>操作</td>
      </tr>
      <tr v-for="item in shopList" class="shop-listinfo">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.price}}</td>
        <td><button @click="addToCart(item)">加入購物車</button></td>
      </tr>
    </table>
  </div>
</template>
<script>
  import{mapActions} from "vuex";
  export default{
    name:'shopList',
    data(){
      return{
      }
    },
    computed:{
      shopList(){
        return this.$store.getters.getShopList
      }
    },
    methods:{
      ...mapActions(['addToCart'])
    }
  }
</script>
<style lang="less" scoped>
  @import url('../../static/public.less');
</style>

選中商品列表

<template>
  <div class="shop-list">
    <table>
      <tr class="shop-listtitle">
        <td>id</td>
        <td>名稱</td>
        <td>價格</td>
        <td>數量</td>
        <td>操作</td>
      </tr>
      <tr v-for="item in cartData" class="shop-listinfo">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.price}}</td>
        <td>{{item.num}}</td>
        <td><button class="shop-dele dele-btn" @click="deletShop(item)">刪除</button></td>
      </tr>
      <tr v-if="cartData.length<=0">
        <td colspan="5">暫無數據</td>
      </tr>
      <tr>
        <td colspan="2">總數:{{totalNum}}</td>
        <td colspan="2">總價格:{{totalPrice}}</td>
        <td><button class="dele-cart dele-btn" @click="clearCart">清空購物車</button></td>
      </tr>
    </table>
  </div>
</template>
<script>
  import {mapGetters,mapActions} from "vuex";
  export default{
    name:'shopCart',
    data(){
      return{
      }
    },
    computed:{
      ...mapGetters({
        cartData:'addShopList',
        totalNum:'totalNum',
        totalPrice:'totalPrice'
      })
    },
    methods:{
      ...mapActions({
        clearCart:'clearToCart',
        deletShop:'deletToShop'
      })
    }
  }
</script>
<style lang="less" scoped>
  @import url('../../static/public.less');
  .dele-btn{
    background-color: red !important;
  }
  .dele-btn:hover{
    background-color: #bd0000 !important;
  }
</style>

vuex 創(chuàng)建

npm install vuex --save,創(chuàng)建vuex文件夾,在文件夾中創(chuàng)建store.js,引入vuex;

import Vue from "vue";
import Vuex from 'vuex';
import cart from "./modules/cart";
Vue.use(Vuex);
export default new Vuex.Store({
  modules:{
    cart
  }
})

建立一個模塊文件夾modules,里面創(chuàng)建創(chuàng)建當個store模塊,然后默認輸出,在store.js中引入;

const state = {
  shop_list: [{
    id: 11,
    name: '魚香肉絲',
    price: 12,
  }, {
    id: 22,
    name: '宮保雞丁',
    price: 14
  }, {
    id: 34,
    name: '土豆絲',
    price: 10
  }, {
    id: 47,
    name: '米飯',
    price: 2
  },{
    id: 49,
    name: '螞蟻上樹',
    price: 13
  },
  {
    id: 50,
    name: '臘肉炒蒜薹',
    price: 15
  }],
  add:[]
}
const getters ={
  //獲取商品列表
  getShopList:state => state.shop_list,
  //獲取購物車列表
  addShopList:state => {
    return state.add.map(({id,num})=>{
      let product = state.shop_list.find(n => n.id == id);
      if(product){
        return{
          ...product,
          num
        }
      }
    })
  },
  //獲取總數量
  totalNum:(state,getters) =>{
    let total =0;
    getters.addShopList.map(n=>{
      total += n.num;
    })
    return total;
  },
  //計算總價格
  totalPrice:(state,getters)=>{
    let total=0;
    getters.addShopList.map(n=>{
      total += n.num*n.price
    })
    return total;
  },
}
const actions={
  //加入購物車
  addToCart({commit},product){
    commit('addCart',{
      id:product.id
    })
  },
  //清空購物車
  clearToCart({commit}){
    commit('clearCart')
  },
  //刪除單個物品
  deletToShop({commit},product){
    commit('deletShop',product)
  }
}
const mutations ={
  //加入購物車
  addCart(state,{id}){
    let record = state.add.find(n => n.id == id);
    if(!record){
      state.add.push({
        id,
        num:1
      })
    }else{
      record.num++;
    }
  },
  //刪除單個物品
  deletShop(state,product){
    state.add.forEach((item,i)=>{
      if(item.id == product.id){
        state.add.splice(i,1)
      }
    })
  },
  //清空購物車
  clearCart(state){
    state.add=[];
  }
}
export default{
  state,
  getters,
  actions,
  mutations
}

希望本文所述對大家vue.js程序設計有所幫助。

相關文章

  • 在Vue中使用防抖與節(jié)流,及this指向的問題

    在Vue中使用防抖與節(jié)流,及this指向的問題

    這篇文章主要介紹了在Vue中使用防抖與節(jié)流,及this指向的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • vue+elementUI實現圖片上傳功能

    vue+elementUI實現圖片上傳功能

    這篇文章主要為大家詳細介紹了vue+elementUI實現圖片上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Vue3組件庫框架搭建example環(huán)境的詳細教程

    Vue3組件庫框架搭建example環(huán)境的詳細教程

    這篇文章主要介紹了Vue3組件庫框架搭建example環(huán)境的詳細教程,本文便搭建?example?開發(fā)環(huán)境和打包構建,并在example中使用組件庫,需要的朋友可以參考下
    2022-11-11
  • vue + el-form 實現的多層循環(huán)表單驗證

    vue + el-form 實現的多層循環(huán)表單驗證

    這篇文章主要介紹了vue + el-form 實現的多層循環(huán)表單驗證,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下。
    2020-11-11
  • uniapp模仿微信實現聊天界面的示例代碼

    uniapp模仿微信實現聊天界面的示例代碼

    這篇文章主要介紹了如何利用uniapp模仿微信,實現一個聊天界面。文中的示例代碼講解詳細,對我們學習Vue有一定的幫助,感興趣的可以了解一下
    2022-01-01
  • 利用Vue v-model實現一個自定義的表單組件

    利用Vue v-model實現一個自定義的表單組件

    本篇文章主要介紹了利用Vue v-model實現一個自定義的表單組件的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • vue使用axios獲取不到響應頭Content-Disposition的問題及解決

    vue使用axios獲取不到響應頭Content-Disposition的問題及解決

    這篇文章主要介紹了vue使用axios獲取不到響應頭Content-Disposition的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • vue 引用自定義ttf、otf、在線字體的方法

    vue 引用自定義ttf、otf、在線字體的方法

    這篇文章主要介紹了vue 引用自定義ttf、otf、在線字體的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • 關于uniapp的高級表單組件mosowe-form

    關于uniapp的高級表單組件mosowe-form

    這篇文章主要介紹了關于uniapp的高級表單組件mosowe-form,由于一些表單標簽改來改去比較繁瑣,重復性很多,且樣式布局啥的幾乎萬變不離其中,為了偷懶,開發(fā)了mosowe-form及mosowe-table兩款高級組件,需要的朋友可以參考下
    2023-04-04
  • vue時間格式總結以及轉換方法詳解

    vue時間格式總結以及轉換方法詳解

    項目中后臺返回的時間有多種形式,時間戳、ISO標準時間格式等,我們需要轉化展示成能看的懂得時間格式,下面這篇文章主要給大家介紹了關于vue時間格式總結以及轉換方法的相關資料,需要的朋友可以參考下
    2022-12-12

最新評論