Vue 간이 카 트 사례 실현

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

CSS 부분
여 기 는 할 말 이 없다.바로 v-cloak 이라는 지식 점 이다.

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;
}
[v-cloak]{
  display: none;
}
HTML 부분
여기에 사 용 된 Vue 의 지식 포 인 트 를 설명 합 니 다.
  • v-if
  • v-for
  • v-cloak
  • v-on > @
  • v-bind > :
  • 방법 methods계산 속성 computed필터 필터
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>   </title>
      <link rel="stylesheet" href="style.css" >
    </head>
    <body>
      
      <div id="app" v-cloak>
        <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">
                <th>{{item.id}}</th>
                <th>{{item.name}}</th>
                <th>{{item.date}}</th>
                <!--              -->
                <!-- <th>{{"¥"+item.price.toFixed(2)}}</th> -->
                <!--   -->
                <!-- <th>{{getFinalPrice(item.price)}}</th> -->
                <!--   -->
                <th>{{item.price | showPrice}}</th>
                <th>
                  <button @click="decrement(index)" :disabled="item.count<=0">-</button>
                  {{item.count}}
                  <button @click="increment(index)">+</button>
                </th>
                <th><button @click="removeHandle(index)">  </button></th>
              </tr>
            </tbody>
          </table>
          <h2>   :{{totalPrice | showPrice}}</h2>
        </div>
        <h2 v-else>
               
        </h2>
      </div>
    
    </body>
    <script src="../js/vue.js"></script>
    <script src="main.js"></script>
    </html>
    JS 부분
    
    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:50.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){
          this.books[index].count++
        },
        decrement(index){
          this.books[index].count--
        },
        removeHandle(index){
          this.books.splice(index,1);
        }
    
      },
      computed: {
        totalPrice(){
          //    :   for  
          // let totalPrice = 0;
          // for(let i=0;i<this.books.length;i++){
          //   totalPrice += this.books[i].price * this.books[i].count
          // }
          // return totalPrice
    
          //    :for in
          // let totalPrice = 0;
          // for(let i in this.books){
          //   // console.log(i);//1 2 3 4
          //   totalPrice += this.books[i].price * this.books[i].count
          // }
          // return totalPrice
    
          //    :for of
          // let totalPrice = 0;
          // for(let item of this.books){
          //   // console.log(item);//               
          //   totalPrice += item.price * item.count
          // }
          // return totalPrice
    
          //    :reduce
          return this.books.reduce(function (preValue, book) {
            // console.log(book);//        
            return preValue + book.price * book.count
          }, 0)
        }
      },
      //    
      filters:{
        showPrice(price){
          return "¥"+price.toFixed(2)
        }
      }
    })
    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기