vue v-if v-else-if v-else 在简单场景中使用computed计算属性的替换写法

<template>
  <div class="hello">
    <!-- 写法1 -->
    <div>
      <label v-if="count < 0">我还没出生</label>
      <label v-else-if="count === 0">我诞生了</label>
      <label v-else-if="count <= 3">我还是个婴儿</label>
      <label v-else-if="count < 15">我还是个少年</label>
      <label v-else>我突然间长大了</label>
    </div>

    <!-- 写法2 -->
    <div>
      <label>{{countMsg}}</label>
    </div>

  </div>
</template>

<script>


export default {
  name: 'HelloWorld',
  data () {
    return {
      count: -21
    }
  },
  methods: {

  },

  computed: {
    // 写法2
    countMsg () {
      if (this.count < 0) {
        return '我还没出生'
      } else if (this.count === 0) {
        return '我诞生了'
      } else if (this.count <= 3) {
        return '我还是个婴儿'
      } else if (this.count < 15) {
        return '我还是个少年'
      } else {
        return '我突然间长大了'
      }
    }
  },

  async created () {

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值