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

Recommend

Vue implements star rating with decimal points

This article shares the specific code of Vue to i...

How to fill items in columns in CSS Grid Layout

Suppose we have n items and we have to sort these...

Solution to css3 transform transition jitter problem

transform: scale(); Scaling will cause jitter in ...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

Detailed explanation of CSS sticky positioning position: sticky problem pit

Preface: position:sticky is a new attribute of CS...

Functions in TypeScript

Table of contents 1. Function definition 1.1 Func...

In-depth analysis of MySQL index data structure

Table of contents Overview Index data structure B...

JavaScript code to achieve a simple calendar effect

This article shares the specific code for JavaScr...

HTML drag and drop function implementation code

Based on Vue The core idea of ​​this function is ...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

Summary of ten principles for optimizing basic statements in MySQL

Preface In the application of database, programme...

Linux installation MongoDB startup and common problem solving

MongoDB installation process and problem records ...

jQuery implements shopping cart function

This article example shares the specific code of ...

Summary of special processing statements of MySQL SQL statements (must read)

1. Update the entire table. If the value of a col...