最终效果:

用于计算最终价格,对此计算使用计算属性最佳
原理是遍历books中的每一个属性,价格*数量
- computed:{
- totalPrice()
- {
- let totalPrice=0
- // 1.普通的for循环
- // for (let i=0;i
- // {
- // totalPrice+=this.books[i].count*this.books[i].price
- // }
- // return totalPrice
-
- // 2.1
- // for(let i in this.books)
- // {
- // totalPrice+=this.books[i].count*this.books[i].price
- // }
-
- // 2.2
- // for(let i in this.books)
- // {
- // const book=this.books[i]
- // totalPrice+=book.count*book.price
- // }
-
- //3.
- for(let item of this.books)
- {
- totalPrice+=item.price*item.count
- }
- }
- },
使每个出现数字的地方自动加上小数点,以及¥
一旦设置完毕,使用非常方便
- filters: {
- showPrice(price) {
- return "¥" + price.toFixed(2)
- }
- }
<h2>总价格:{{totalPrice|showPrice}}h2>
按钮动态绑定disabled属性,当数量为1时不可再减少,此时禁用减号按钮
<button @click="decrement(index)" v-bind:disabled="item.count<=1">-button>
当所有书都被移除时,显示购物车为空
即只有书的种类不为空(length>=1)时渲染元素
当为空是显示购物车为空
<div v-if="books.length">
<h2 v-else>购物车为空h2>
当点击按钮时,书的数量变化
当点击移除时,去掉书的这一行
<button @click="increment(index)">+button>
<td><button @click="removeHandler(index)">移除button>td>
- decrement(index) {
- // console.log("decrement",index)
- this.books[index].count--
- },
-
- // 实现移除功能:splice
- removeHandler(index) {
- this.books.splice(index, 1)
- }
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- <link rel="stylesheet" href="style.css">
- head>
- <body>
-
- <div id="app">
- <div v-if="books.length">
- <table>
- <thead>
- <tr>
- <th>th>
- <th>书籍名称th>
- <th>出版日期th>
- <th>价格th>
- <th>购买数量th>
- <th>操作th>
- tr>
- thead>
- <tbody>
- <tr v-for="(item,index) in books">
- <td>{{item.id}}td>
- <td>{{item.name}}td>
- <td>{{item.date}}td>
- <td>{{item.price | showPrice}}td>
- <td>
- <button @click="decrement(index)" v-bind:disabled="item.count<=1">-button>
- {{item.count}}
- <button @click="increment(index)">+button>
- td>
- <td><button @click="removeHandler(index)">移除button>td>
- tr>
- tbody>
- table>
- <h2>总价格:{{totalPrice|showPrice}}h2>
- div>
- <h2 v-else>购物车为空h2>
- div>
-
- <script src="../js/vue.js">script>
- <script src="main.js">script>
-
-
- body>
- html>

- const app=new Vue(
- {
- el: "#app",
- data: {
- books: [
- {
- id: 1,
- name: "算法导论",
- date: "2006-9",
- price: 85.00,
- count: 1
- },
- {
- id: 2,
- name: "UNIX编程艺术",
- date: "2006-2",
- price: 59.00,
- count: 1
- }, {
- id: 3,
- name: "编程珠玑",
- date: "2008-10",
- price: 39.00,
- count: 1
- }, {
- id: 4,
- name: "代码大全",
- date: "2006-3",
- price: 128.00,
- count: 1
- },
- ],
- },
- methods: {
- // getFinalPrice(price)
- // {
- // return "¥"+price.toFixed(2)
- // }
- //因为有多个按钮,必须根据下标判断具体是哪一个
- increment(index) {
- // console.log("increment",index)
- this.books[index].count++
- },
- decrement(index) {
- // console.log("decrement",index)
- this.books[index].count--
- },
-
- // 实现移除功能:splice
- removeHandler(index) {
- this.books.splice(index, 1)
- }
- },
- computed:{
- totalPrice()
- {
- let totalPrice=0
- for (let i=0;i<this.books.length;i++)
- {
- totalPrice+=this.books[i].count*this.books[i].price
- }
- return totalPrice
- }
- },
- filters: {
- showPrice(price) {
- return "¥" + price.toFixed(2)
- }
- }
- }
-
- )
- table{
- border: 1px solid #e9e9e9;
- border-collapse: collapse;
- border-spacing: 0;
- }
-
- th,td{
- padding: 8px 16px;
- border: 1px solid #e9e9e9;
- text-align: left;
- }
-
- th{
- background-color: #f7f7f7;
- color: #5c6c77;
- font-weight: 600;
- }