vue 단순 카 트 사례 실현

4694 단어 vue쇼핑 카 트
본 논문 의 사례 는 vue 가 간단 한 카 트 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

코드:

<!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>  </th>
          <th>    </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>
    <h2 v-if='books!=""'>   :{{theSumOf | getFinalPrice}}</h2>
    <h2 v-else>     </h2>
  </div>
</body>
<script src="../js/vue.min.js"></script>
<script>
  var app = new Vue({
    el: '#app',
    data: {
      books: [
        {
          id: 1,
          name: '     ',
          date: '2006-9',
          price: 85.00,
          count: 1

        },
        {
          id: 2,
          name: 'java  ',
          date: '2006-9',
          price: 10.00,
          count: 1

        },
        {
          id: 3,
          name: '   ',
          date: '2006-9',
          price: 85.00,
          count: 1

        },
        {
          id: 4,
          name: 'ui   ',
          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 () {
      //          
        //let sum = 0
        //this.books.forEach(item => {
        // sum += parseInt(item.price)*parseInt(item.count)
        });
        //return sum

 //          
 //let sum = 0
 //for(let i in this.books){
  //sum += this.books[i].price*this.books[i]*count
 }
 //return sum

 //          
 //let sum = 0
 //for(let item of this.books){
 //sum += item.price*item.count
 //}
 //return sum

 //          
 return this.books.reduce(function(preValue,book){
 return preValue + book.price*book.count
 },0)
      }

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

<html>
vue.js 의 학습 튜 토리 얼 에 대해 서 는 주제vue.js 구성 요소 학습 강좌,Vue.js 전단 구성 요소 학습 튜 토리 얼를 클릭 하여 공부 하 십시오.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기