Implementing shopping cart function based on vuex

Implementing shopping cart function based on vuex

This article example shares the specific code of vuex to implement the shopping cart function for your reference. The specific content is as follows

First look at the effect:

Code:

<template>
 <div class="Home">
 <h1>Vuex shopping cart example</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>

Parent Component

<template>
 <div class="add">
 <div class="from-group">
 <label for="">Product Number</label>
 <input type="text" v-model="shop.id" placeholder="Please enter the product number">
 </div>
 <div class="from-group">
 <label for="">Product Name</label>
 <input type="text" v-model="shop.name" placeholder="Please enter the product name">
 </div>
 <div class="from-group">
 <label for="">Product Price</label>
 <input type="text" v-model="shop.price" placeholder="Please enter the product price">
 </div>
 <div class="from-group">
 <label for="">Quantity of product</label>
 <input type="text" v-model="shop.count" placeholder="Please enter the quantity of the product">
 </div>
 <div class="from-group">
 <button @click="add">Add product</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

<template>
 <div class="Shop-Cart">

 <table>
 <thead border>
 <tr>
  <th>Product Number</th>
  <th>Product Name</th>
  <th>Product price</th>
  <th>Quantity of goods</th>
  <th>Subtotal</th>
  <th>Operation</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)">Delete</button></td>
 </tr>
 </tbody>
 <tfoot>
 <tr>
  <td colspan="6" align="right">Total: {{ total }}</td>
  <button @click="clear">Clear Shopping Cart</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);
 },
 delete(e) {
 this.$store.dispatch('deleteList', e);
 },

 clear() {
 this.$store.dispatch('clearList', []);
 }
 }
};
</script>

vuex components

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


export default new Vuex.Store({
 state: {
 list: [{
 id: 1,
 name: "Wow, haha",
 price: 3,
 count: 0
 },
 {
 id: 2,
 name: "Wowhaha",
 price: 3,
 count: 0
 }
 ]
 },
 getters: {
 //Get shopping cart data getlist(state) {
 return state.list
 },
 //Total price of the product getShopTotal(state,index) {
 let result = 0;
 state.list.forEach((item, index) => {
 result += item.price * item.count
 })
 return result
 },
 },
 mutations:
 //Delete a single data in the shopping cart remoteList(state,index) {
 state.list.splice(index, 1);
 console.log("aaa",state)
 },
 //Increase the number of products addList(state, index) {
 state.list[index].count++;
 },
 //Decrease the number of products deleteList(state, index) {
 state.list[index].count--;
 if(state.list[index].count<=0){
 state.list[index].count = 0
 return ;
 }
 },

 // Clear the shopping cart clearList(state, arr) {
 state.list = arr
 },
 addShopList(state,shop){
 state.list.push(shop)
 }
 },
 //Use actions to call mutations method 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: {}
})

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vuex implements a simple shopping cart
  • Vuex implements small shopping cart function
  • Vuex implements adding, reducing and removing shopping carts
  • Vuex realizes shopping cart function
  • Example code for using vuex to elegantly implement a shopping cart function
  • Simple shopping cart function example implemented by vuex
  • I wrote a sample code for a shopping cart H5 page using vuex
  • Vuex implements simple shopping cart function

<<:  Implementing carousel with native JavaScript

>>:  An example of implementing a simple infinite loop scrolling animation in Vue

Blog    

Recommend

Detailed explanation of JS array methods

Table of contents 1. The original array will be m...

Implementation of vue-nuxt login authentication

Table of contents introduce Link start Continue t...

How to encapsulate the table component of Vue Element

When encapsulating Vue components, I will still u...

Six-step example code for JDBC connection (connecting to MySQL)

Six steps of JDBC: 1. Register the driver 2. Get ...

Detailed installation and configuration of MySql on Mac

1. Download and install Download the community ed...

Detailed example of removing duplicate data in MySQL

Detailed example of removing duplicate data in My...

Html easily implements rounded rectangle

Question: How to achieve a rounded rectangle usin...

Summary of webpack's mobile adaptation solution

Table of contents rem vw Adapt to third-party UI ...

Classes in TypeScript

Table of contents 1. Overview 2. Define a simple ...

MySQL query statement grouped by time

MySQL query by year, month, week, day group 1. Qu...

Detailed explanation of Vue login and logout

Table of contents Login business process Login fu...

How to deploy Confluence and jira-software in Docker

version: centos==7.2 jdk==1.8 confluence==6.15.4 ...

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

How to optimize MySQL deduplication operation to the extreme

Table of contents 1. Clever use of indexes and va...