国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - vue.js - 基于vuex實現購物車功能

基于vuex實現購物車功能

2021-12-29 16:06棟棟很優秀啊 vue.js

這篇文章主要為大家詳細介紹了基于vuex實現購物車功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vuex實現購物車功能的具體代碼,供大家參考,具體內容如下

先看效果:

基于vuex實現購物車功能

代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<template>
 <div class="Home">
 <h1>vuex購物車案例</h1>
 <add-from></add-from>
 <shop-cart></shop-cart>
 </div>
</template>
 
<script>
import AddFrom from './Add.vue'
import ShopCart from './ShopCart.vue'
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
export default {
 name: 'Home',
 components: {
 AddFrom,
 ShopCart
 },
 
};
</script>
<style>
table {
 width: 800px;
 margin: 0 auto;
 border: 1px solid #ccc;
 border-spacing: 0;
}
tbody th,
tbody td {
 border: 1px solid #ccc;
 text-align: center;
}
h1{
 text-align: center;
}
.add{
 width: 800px;
 margin: 0 auto;
}
</style>

父組件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<template>
 <div class="add">
 <div class="from-group">
 <label for="">商品編號</label>
 <input type="text" v-model="shop.id" placeholder="請輸入商品編號">
 </div>
 <div class="from-group">
 <label for="">商品名稱</label>
 <input type="text" v-model="shop.name" placeholder="請輸入商品名稱">
 </div>
 <div class="from-group">
 <label for="">商品價格</label>
 <input type="text" v-model="shop.price" placeholder="請輸入商品價格">
 </div>
 <div class="from-group">
 <label for="">商品數量</label>
 <input type="text" v-model="shop.count" placeholder="請輸入商品數量">
 </div>
 <div class="from-group">
 <button @click="add">添加商品</button>
 </div>
 </div>
</template>
 
<script>
export default {
 name: 'add',
 data() {
 return {
 shop:{}
 };
 },
 methods:{
 add(){
 
 this.$store.dispatch("addShopList",this.shop)
 this.shop = {
 id:"",
 name:"",
 price:"",
 count:""
 }
 }
 }
};
</script>
 
<style scoped>
 .add{
 width: 800px;
 text-align: center;
 }
</style>

```bash

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
 <div class="Shop-Cart">
 
 <table>
 <thead border>
 <tr>
  <th>商品編號</th>
  <th>商品名稱</th>
  <th>商品價格</th>
  <th>商品數量</th>
  <th>小計</th>
  <th>操作</th>
 </tr>
 </thead>
 <tbody v-if="shop.length > 0">
 <tr v-for="(i, e) in shop" :key="e">
  <td>{{ i.id }}</td>
  <td>{{ i.name }}</td>
  <td>{{ i.price }}</td>
  <td>
  <button @click="add(e)">+</button>
  <input type="text" v-model="i.count" />
  <button @click="delet(e)">-</button>
  </td>
  <td>¥{{ i.price * i.count }}</td>
  <td><button @click="del(e)">刪除</button></td>
 </tr>
 </tbody>
 <tfoot>
 <tr>
  <td colspan="6" align="right">總計:{{ total }}</td>
  <button @click="clear">清除購物車</button>
 </tr>
 </tfoot>
 </table>
 </div>
</template>
 
<script>
export default {
 name: 'Shop-Cart',
 components: {},
 data() {
 return {};
 },
 computed: {
 shop() {
 return this.$store.getters.getlist;
 },
 total() {
 return this.$store.getters.getShopTotal;
 }
 },
 methods: {
 del(e) {
 // this.$store.dispatch()
 this.$store.dispatch('remoteList', e);
 },
 
 add(e) {
 this.$store.dispatch('addList', e);
 },
 delet(e) {
 this.$store.dispatch('deleteList', e);
 },
 
 clear() {
 this.$store.dispatch('clearList', []);
 }
 }
};
</script>

vuex組件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import Vue from 'vue'
import Vuex from 'vuex'
 
Vue.use(Vuex)
 
 
export default new Vuex.Store({
 state: {
 list: [{
 id: 1,
 name: "哇哈哈",
 price: 3,
 count: 0
 },
 {
 id: 2,
 name: "哇哈",
 price: 3,
 count: 0
 }
 ]
 },
 getters: {
 //獲取購物車數據
 getlist(state) {
 return state.list
 },
 //商品的總價
 getShopTotal(state,index) {
 let result = 0;
 state.list.forEach((item, index) => {
 result += item.price * item.count
 })
 return result
 },
 },
 mutations: {
 //刪除購物車單個數據
 remoteList(state,index) {
 state.list.splice(index, 1);
 console.log("aaa",state)
 },
 //商品數量增加
 addList(state, index) {
 state.list[index].count++;
 },
 //商品數量減少
 deleteList(state, index) {
 state.list[index].count--;
 if(state.list[index].count<=0){
 state.list[index].count = 0
 return ;
 }
 },
 
 //清除購物車
 clearList(state, arr) {
 state.list = arr
 },
 addShopList(state,shop){
 state.list.push(shop)
 }
 },
 //使用actions調用mutations方法
 actions: {
 remoteList({
 commit
 }, index) {
 commit("remoteList", index)
 },
 addList({
 commit
 }, index) {
 commit("addList", index)
 },
 deleteList({
 commit
 }, index) {
 commit("deleteList", index)
 },
 clearList({
 commit
 }, arr) {
 commit("clearList", arr)
 },
 addShopList({commit},shop){
 commit("addShopList",shop)
 }
 },
 modules: {}
})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/dongdongaa0/article/details/111304266

延伸 · 閱讀

精彩推薦
  • vue.jsVue項目中實現帶參跳轉功能

    Vue項目中實現帶參跳轉功能

    最近做了一個手機端系統,其中遇到了父頁面需要攜帶參數跳轉至子頁面的問題,現已解決,下面分享一下實現過程,感興趣的朋友一起看看吧...

    YiluRen丶4302022-03-03
  • vue.jsVue2.x 項目性能優化之代碼優化的實現

    Vue2.x 項目性能優化之代碼優化的實現

    這篇文章主要介紹了Vue2.x 項目性能優化之代碼優化的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋...

    優小U9632022-02-21
  • vue.js梳理一下vue中的生命周期

    梳理一下vue中的生命周期

    看過很多人講vue的生命周期,但總是被繞的云里霧里,尤其是自學的同學,可能js的基礎也不是太牢固,聽起來更是吃力,那我就已個人之淺見,以大白話...

    CRMEB技術團隊7992021-12-22
  • vue.js用vite搭建vue3應用的實現方法

    用vite搭建vue3應用的實現方法

    這篇文章主要介紹了用vite搭建vue3應用的實現方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下...

    Asiter7912022-01-22
  • vue.jsVue中引入svg圖標的兩種方式

    Vue中引入svg圖標的兩種方式

    這篇文章主要給大家介紹了關于Vue中引入svg圖標的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的...

    十里不故夢10222021-12-31
  • vue.jsVue多選列表組件深入詳解

    Vue多選列表組件深入詳解

    這篇文章主要介紹了Vue多選列表組件深入詳解,這個是vue的基本組件,有需要的同學可以研究下...

    yukiwu6752022-01-25
  • vue.jsVue2.x-使用防抖以及節流的示例

    Vue2.x-使用防抖以及節流的示例

    這篇文章主要介紹了Vue2.x-使用防抖以及節流的示例,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下...

    Kyara6372022-01-25
  • vue.js詳解vue 表單綁定與組件

    詳解vue 表單綁定與組件

    這篇文章主要介紹了vue 表單綁定與組件的相關資料,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下...

    Latteitcjz6432022-02-12
主站蜘蛛池模板: 奇米av在线 | 欧美伦理电影一区二区 | 日韩在线观看成人 | 婷婷综合在线 | 欧美黄页 | 欧美精产国品一二三区 | 成人免费视频网站在线观看 | 国产一区久久 | 久久久久久国产一级毛片高清版 | 国产精品一区二区三区免费 | 一级片网址 | 久久亚洲欧美日韩精品专区 | 国语精品久久 | 亚洲综合射 | 交视频在线观看国产 | 日韩在线中文 | 日韩在线免费 | 性色aⅴ免费视频 | 久久久激情视频 | 91亚洲国产成人久久精品网站 | 91亚洲国产成人久久精品网站 | 成人免费视频在线观看 | 亚洲精品综合 | 日韩国产一区二区三区 | 日本一区二区三区四区 | 免费自拍偷拍视频 | 91久久精品国产91久久性色tv | 欧美1页 | 红桃av一区二区 | 男人天堂网址 | 黄色免费在线观看 | 狠狠干最新网址 | 精品在线视频播放 | 国产性猛交xxxx免费看久久 | 国产精品久久久久久久久小说 | 亚洲视频在线看 | 色综久久 | 日韩在线一区二区三区 | 在线视频a | 亚洲国产精品久久久 | 日韩福利 |