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

vuex實(shí)現(xiàn)的簡(jiǎn)單購物車功能示例

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

本文實(shí)例講述了vuex實(shí)現(xiàn)的簡(jiǎn)單購物車功能。分享給大家供大家參考,具體如下:

購物車組件

<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>價(jià)格</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>價(jià)格</td>
        <td>數(shù)量</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">暫無數(shù)據(jù)</td>
      </tr>
      <tr>
        <td colspan="2">總數(shù):{{totalNum}}</td>
        <td colspan="2">總價(jià)格:{{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
  }
})

建立一個(gè)模塊文件夾modules,里面創(chuàng)建創(chuàng)建當(dāng)個(gè)store模塊,然后默認(rèn)輸出,在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
        }
      }
    })
  },
  //獲取總數(shù)量
  totalNum:(state,getters) =>{
    let total =0;
    getters.addShopList.map(n=>{
      total += n.num;
    })
    return total;
  },
  //計(jì)算總價(jià)格
  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')
  },
  //刪除單個(gè)物品
  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++;
    }
  },
  //刪除單個(gè)物品
  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
}

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • vite項(xiàng)目如何集成eslint和prettier

    vite項(xiàng)目如何集成eslint和prettier

    這篇文章主要介紹了vite項(xiàng)目如何集成eslint和prettier問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • vue實(shí)現(xiàn)檢測(cè)敏感詞過濾組件的多種思路

    vue實(shí)現(xiàn)檢測(cè)敏感詞過濾組件的多種思路

    這篇文章主要介紹了vue編寫檢測(cè)敏感詞匯組件的多種思路,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04
  • vue-cli對(duì)element-ui組件進(jìn)行二次封裝的實(shí)戰(zhàn)記錄

    vue-cli對(duì)element-ui組件進(jìn)行二次封裝的實(shí)戰(zhàn)記錄

    組件類似于需要多個(gè)地方用到的方法,在Vue中組件就是一種復(fù)用(經(jīng)常使用)一個(gè)功能的手段,下面這篇文章主要給大家介紹了關(guān)于Vue?element?ui二次封裝的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Vue3中watch監(jiān)聽器及源碼學(xué)習(xí)

    Vue3中watch監(jiān)聽器及源碼學(xué)習(xí)

    本文主要介紹了Vue3中watch監(jiān)聽器及源碼學(xué)習(xí),Watch偵聽器在Vue3中特性進(jìn)行了一些改變和優(yōu)化,下面來詳解的介紹一下基本使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • vue引入外部的js文件的10種方法總結(jié)

    vue引入外部的js文件的10種方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目中引入外部的js文件的10種方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下
    2023-08-08
  • vue 全局引用公共的組件以及公共的JS文件問題

    vue 全局引用公共的組件以及公共的JS文件問題

    這篇文章主要介紹了vue 全局引用公共的組件以及公共的JS文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue狀態(tài)管理庫Pinia詳細(xì)介紹

    Vue狀態(tài)管理庫Pinia詳細(xì)介紹

    這篇文章主要介紹了Vue3-pinia狀態(tài)管理,pinia是 vue3 新的狀態(tài)管理工具,簡(jiǎn)單來說相當(dāng)于之前 vuex,它去掉了 Mutations 但是也是支持 vue2 的,需要的朋友可以參考下
    2022-08-08
  • 前端面試之vue2和vue3的區(qū)別有哪些

    前端面試之vue2和vue3的區(qū)別有哪些

    這篇文章主要為大家介紹了前端面試之vue2和vue3的區(qū)別有哪些,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • vue+elementUI組件table實(shí)現(xiàn)前端分頁功能

    vue+elementUI組件table實(shí)現(xiàn)前端分頁功能

    這篇文章主要為大家詳細(xì)介紹了vue+elementUI組件table實(shí)現(xiàn)前端分頁功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 詳解Vue 路由組件傳參的 8 種方式

    詳解Vue 路由組件傳參的 8 種方式

    這篇文章主要介紹了Vue 路由組件傳參的 8 種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03

最新評(píng)論

太白县| 阿鲁科尔沁旗| 南雄市| 沁源县| 武平县| 拉孜县| 长岛县| 青阳县| 安阳县| 清镇市| 城固县| 盘锦市| 徐水县| 本溪| 叶城县| 侯马市| 杭锦后旗| 上栗县| 隆化县| 景泰县| 哈密市| 墨玉县| 伊川县| 连城县| 惠水县| 张家港市| 博客| 东明县| 沈丘县| 宁安市| 改则县| 冷水江市| 资兴市| 渭南市| 舒城县| 华坪县| 庐江县| 大竹县| 呼图壁县| 揭东县| 广昌县|