This article example shares the specific code of Vue to implement a simple shopping cart for your reference. The specific content is as follows Code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"> <table> <thead> <tr> <th></th> <th>Book Title</th> <th>Publication Date</th> <th>Price</th> <th>Purchase quantity</th> <th>Operation</th> </tr> </thead> <tbody> <tr v-for='(item,index) in books' ::key="item"> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.date}}</td> <td>{{item.price*item.count | getFinalPrice}}</td> <td> <button @click='reduce(index)' :disabled='item.count <= 1'>-</button> {{item.count}} <button @click='add(index)'>+</button> </td> <td> <button @click='removeBtn(index)'>Remove</button> </td> </tr> </tbody> </table> <h2 v-if='books!=""'>Total price:{{theSumOf | getFinalPrice}}</h2> <h2 v-else>Shopping cart is empty</h2> </div> </body> <script src="../js/vue.min.js"></script> <script> var app = new Vue({ el: '#app', data: { books: { id: 1, name: 'Computer Applications', date: '2006-9', price: 85.00, count: 1 }, { id: 2, name: 'java application', date: '2006-9', price: 10.00, count: 1 }, { id: 3, name: 'Big Data', date: '2006-9', price: 85.00, count: 1 }, { id: 4, name: 'ui designer', date: '2006-9', price: 85.00, count: 1 }, ], addAnd:0 }, methods: { add(index) { this.books[index].count++ }, reduce(index) { this.books[index].count-- }, removeBtn(index) { this.books.splice(index, 1) } }, components: }, computed: { theSumOf: function () { //The first method of cumulative calculation //let sum = 0 //this.books.forEach(item => { // sum += parseInt(item.price)*parseInt(item.count) }); //return sum //The second way of cumulative calculation //let sum = 0 //for(let i in this.books){ //sum += this.books[i].price*this.books[i]*count } //return sum //The third way of cumulative calculation //let sum = 0 //for(let item of this.books){ //sum += item.price*item.count //} //return sum //The fourth method of cumulative calculation return this.books.reduce(function(preValue,book){ return preValue + book.price*book.count },0) } }, filters: getFinalPrice(price) { return '¥' + price.toFixed(2) }, } }); </script> <html> Regarding the learning tutorial of vue.js, please click on the special topics vue.js component learning tutorial and Vue.js front-end component learning tutorial for learning. 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:
|
<<: MySQL 8.0.13 installation and configuration method graphic tutorial under win10
>>: A brief analysis of different ways to configure static IP addresses in RHEL8
Overview MySQL also has its own event scheduler, ...
System environment: Windows 7 1. Install Docker D...
Sorting query (order by) In e-commerce: We want t...
1 Overview System centos8, use httpd to build a l...
Operating system: Window10 MySQL version: 8.0.13-...
Sometimes, we don't want the content presente...
Table of contents Update the image from an existi...
1. Download MySQL Workbench Workbench is a graphi...
Today we are going to implement a Thunder Fighter...
When the token expires, refresh the page. If the ...
Table of contents Binding Class Binding inline st...
Sometimes you need to access some static resource...
A simple example of how to use the three methods ...
reduce method is an array iteration method. Unlik...
JSON is a lightweight data exchange format that u...