计算属性的复杂使用

41 篇文章 0 订阅

复杂一点的计算属性操作:

<template>
  <div id="computedBox">
      <!-- 使用拼接的方法,可读性不佳,多个的话写起来繁琐 -->
      <h2>{{firstName + ' ' + lastName}}</h2>
      <h2>{{firstName}} {{lastName}}</h2>
      <!-- 可以调用方法显示,不过不够简洁 -->
      <h2>{{getFullName()}}</h2>
      <!-- 计算属性,直接mustache语法显示新的值 -->
      <h2>{{fullName}}</h2>
      <ul>
          <li v-for="(item,index) in books" :key="index">{{item.name}}</li>
      </ul>
      <h2>总价:{{totalPrise}}</h2>
  </div>
</template>

<script>
export default {
  data() {
    return {
       firstName:'Lit',
       lastName:'Jenney',
       books:[
           {
               id:1,name:'明日',prise:150
           },
           {
               id:2,name:'喜洋洋灰太狼',prise:197
           },
           {
               id:3,name:'网球小子',prise:200
           },
           {
               id:4,name:'灌篮高手',prise:130
           }
       ]
    };
  },
//   计算属性,是一个属性
  computed:{
      fullName(){
          return this.firstName + ' ' + this.lastName
      },
      totalPrise(){
          let res = 0 ;
        //   for循环
        //   for (let i=0; i<this.books.length;i++){
        //       res += this.books[i].prise
        //   }

        // for in遍历的是数组的索引(即键名),
          for(let i in this.books){
              // console.log(i)  //索引 0 1 2 3
              // console.log(this.books[i].prise)
              // 取值还需要this.books[i]
              res += this.books[i].prise
          }
          
          //而for of遍历的是数组元素值。直接出来每个对象的value
          // for(let i of this.books){
            //   console.log(i.prise)
            //  res += i.prise
          // }
          return res
      }
  },
  methods:{
    //   通过方法获取值,里面的值需要加this
      getFullName(){
          return this.firstName + ' ' + this.lastName
      }
  }
};
</script>

<style scoped>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值