一个Vue购物车的小案例 不使用CLI

前文说明

主要使用vue.js的本地工具包进行页面编辑,主要使用v-if、v-else、v-for、v-on、v-bind、filters、compute等
vue.js下载链接:https://cn.vuejs.org/v2/guide/installation.html

编写 cart.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>vue-购物车</title>
  <link rel="stylesheet" href="cart.css">
  <!--  <script type="text/javascript" src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>-->
</head>
<body>
<div id="app">
  <!-- 使用v-if来判断购物车是否有物品 -->
  <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>{{getFinalPrice(item.price)}}</td>-->
        <td>{{item.price | showPrice}}</td>
        <td>
          <button @click="decrement(index)" :disabled="item.count <= 1">-</button>
          {{item.count}}
          <button @click="increment(index)">+</button>
        </td>
        <td>
          <button @click="remove(index)">删除</button>
        </td>
      </tr>
      </tbody>
    </table>
    <!-- 过滤器的使用 -->
    <h2>总价格:{{totalPrice | showPrice}}</h2>
  </div>
  <div v-else>
    <h1>购物车为空 请进行购物哦!</h1>
  </div>
</div>
<script src="../js/vue.js"></script>
<script src="main.js"></script>
</body>
</html>

编写 main.js

const app = new Vue({
  el: '#app',
  data: {
    books: [
      {
        id: 1,
        name: '《算法导论》',
        date: '2016-04-10',
        price: 89.00,
        count: 1
      },
      {
        id: 2,
        name: "《vue实战编程》",
        date: "2016-04-10",
        price: 80.00,
        count: 1
      },
      {
        id: 3,
        name: "《ES6》",
        date: "2016-04-10",
        price: 78.00,
        count: 1
      },
      {
        id: 4,
        name: "《UNIX大全》",
        date: "2016-04-10",
        price: 69.00,
        count: 1
      },
    ]
  },
  methods: {
    // 方法1 使用方法进行价格格式化操作
    // getFinalPrice( price) {
    //   return '¥' + price.toFixed(2)
    // }
    decrement(index) {
      this.books[index].count--
    },
    increment(index) {
      this.books[index].count++
    },
    remove(index) {
      this.books.splice(index, 1)
    }
  },
  computed: {
    totalPrice() {
      let totalPrice = 0
      //方法1 使用for循环进行的正常遍历
      // for (let i = 0; i < this.books.length; i++) {
      //   totalPrice += this.books[i].price * this.books[i].count
      // }

      //方法2 for ... in 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作。
      // for (let i in this.books) {
      //   totalPrice += this.books[i].price * this.books[i].count
      // }

      //方法3 for of和forEach一样,是直接得到值,不能对象使用
      for (let item of this.books) {
        totalPrice += item.price * item.count
      }
      return totalPrice
    }
  },
  //过滤器
  filters: {
    // 方法2 使用过滤器进行价格格式化操作
    showPrice(price) {
      return '¥' + price.toFixed(2)
    }
  }
})

编写 cart.css

table {
    border: 1px solid #e9e9e9;
    border-collapse: collapse;
    border-spacing: 0;
}

th, td {
    padding: 8px 16px;
    border: 1px solid #e9e9e9;
    text-align: center;
}

th {
    background-color: #f7f7f7;
    color: #5c6b77;
    font-weight: 600;
}

总结

通过这样一个小的案例来巩固自己对VUE学习,检查自己的掌握情况,记录自己的学习之旅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值