国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 12:09:14
字體:
來源:轉載
供稿:網友

本文實例講述了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 創建

npm install vuex --save,創建vuex文件夾,在文件夾中創建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,里面創建創建當個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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 龙川县| 常山县| 宜宾市| 南投市| 永修县| 湟中县| 法库县| 佛山市| 都安| 卓资县| 太湖县| 鹿邑县| 甘肃省| 杭锦后旗| 涟源市| 陇川县| 鹤壁市| 偃师市| 永新县| 甘泉县| 莱西市| 玉门市| 和林格尔县| 临澧县| 读书| 修武县| 贵溪市| 上饶市| 新宁县| 横山县| 镇宁| 澎湖县| 绍兴县| 桐城市| 大姚县| 临清市| 江川县| 金湖县| 肥东县| 漳州市|