溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用vue怎么實(shí)現(xiàn)簡單購物車

發(fā)布時(shí)間:2021-06-01 16:17:35 來源:億速云 閱讀:194 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用vue怎么實(shí)現(xiàn)簡單購物車,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

代碼:

<!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>書籍名稱</th>
          <th>出版日期</th>
          <th>價(jià)格</th>
          <th>購買數(shù)量</th>
          <th>操作</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)'>移除</button>
          </td>
        </tr>
      </tbody>
    </table>
    <h3 v-if='books!=""'>總價(jià)格:{{theSumOf | getFinalPrice}}</h3>
    <h3 v-else>購物車為空</h3>
  </div>
</body>
<script src="../js/vue.min.js"></script>
<script>
  var app = new Vue({
    el: '#app',
    data: {
      books: [
        {
          id: 1,
          name: '計(jì)算機(jī)應(yīng)用',
          date: '2006-9',
          price: 85.00,
          count: 1

        },
        {
          id: 2,
          name: 'java應(yīng)用',
          date: '2006-9',
          price: 10.00,
          count: 1

        },
        {
          id: 3,
          name: '大數(shù)據(jù)',
          date: '2006-9',
          price: 85.00,
          count: 1

        },
        {
          id: 4,
          name: 'ui設(shè)計(jì)師',
          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 () {
      //累加計(jì)算的第一種方法
        //let sum = 0
        //this.books.forEach(item => {
        // sum += parseInt(item.price)*parseInt(item.count)
        });
        //return sum

 //累加計(jì)算的第二種方式
 //let sum = 0
 //for(let i in this.books){
  //sum += this.books[i].price*this.books[i]*count
 }
 //return sum

 //累加計(jì)算的第三種方式
 //let sum = 0
 //for(let item of this.books){
 //sum += item.price*item.count
 //}
 //return sum

 //累加計(jì)算的第四種方法
 return this.books.reduce(function(preValue,book){
 return preValue + book.price*book.count
 },0)
      }

    },
    filters: {
      getFinalPrice(price) {
        return '¥' + price.toFixed(2)
      },
    }
  });
</script>

<html>

關(guān)于使用vue怎么實(shí)現(xiàn)簡單購物車就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI