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:
|
<<: Implementing carousel with native JavaScript
>>: An example of implementing a simple infinite loop scrolling animation in Vue
This article shares the specific code of Vue to i...
Suppose we have n items and we have to sort these...
transform: scale(); Scaling will cause jitter in ...
Introduction: When I looked at interview question...
Preface: position:sticky is a new attribute of CS...
Table of contents 1. Function definition 1.1 Func...
Table of contents Overview Index data structure B...
This article shares the specific code for JavaScr...
Based on Vue The core idea of this function is ...
The multi-site feature of WordPress allows you to...
Neo4j (one of the Nosql) is a high-performance gr...
Preface In the application of database, programme...
MongoDB installation process and problem records ...
This article example shares the specific code of ...
1. Update the entire table. If the value of a col...