vue中computed屬性的複雜操作

用到的幾種不同for循環寫法
在這裏插入圖片描述

代碼片段

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="../js/vuejs-2.5.16.js"></script>
</head>
<body>
  <div id="app">
    手機總價:{{totalPrice}}
  </div>

</body>
<script>
  const app = new Vue({
    el: '#app',
    data:{
      phones: [
          {id: 100, name: "蘋果手機", price: 11999},
          {id: 101, name: "華爲手機", price: 10999},
          {id: 102, name: "小米手機", price: 3899},
          {id: 103, name: "vivo手機", price: 8999}
          ]
  },
    computed: {
      totalPrice: function () {
        let total =0
        /* ES5 中的for循環寫法*/
       /* for(let i=0;i<this.phones.length;i++){
          total += this.phones[i].price;
        }*/
      /*  for (let i in this.phones){
          total += this.phones[i].price;
        }*/
        for (let phone of this.phones){
          total += phone.price
        }
        return total
      }
    }
  })
</script>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章