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

Use CSS to set the width of INPUT in TD

Recently, when I was using C# to make a Web progra...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

How to implement n-grid layout in CSS

Common application scenarios The interfaces of cu...

Analysis of MySQL example DTID master-slave principle

Table of contents 1. Basic Concepts of GTID 2. GT...

MySQL5.7 master-slave configuration example analysis

MySQL5.7 master-slave configuration implementatio...

Detailed explanation of Cgroup, the core principle of Docker

The powerful tool cgroup in the kernel can not on...

How to quickly build an FTP file service using FileZilla

In order to facilitate the storage and access of ...

The specific use and difference between attribute and property in Vue

Table of contents As attribute and property value...

Alibaba Cloud Server Domain Name Resolution Steps (Tutorial for Beginners)

For novices who have just started to build a webs...

Detailed explanation of the use of find_in_set() function in MySQL

First, let’s take an example: There is a type fie...

What do CN2, GIA, CIA, BGP and IPLC mean?

What is CN2 line? CN2 stands for China Telecom Ne...