본문 바로가기
Frontend/Vue 2

vue3 computed

by SOLYI 2021. 10. 18.


@@@Computed


//계산된 데이터
computed 에 function haSFruits 생성후
v-if문에 hasFruits  입력

function reverseFruits 생성후
v-for문 fruit in reverseFruits 

<template>
  <section v-if="hasFruit">
    <h1>Fruits</h1>
    <ul>
      <li
        v-for="fruit in fruits"
        :key="fruit">
        {{ fruit }}
      </li>
    </ul>
  </section>
  <section>
    <h1>Reverse Fruit</h1>  
    <ul>
      <li
        v-for="fruit in reverseFruits"
        :key="fruit">
        {{ fruit }}
      </li>
    </ul>
  </section>
</template>

<script>
export default {
  data:function(){
    return {
      fruits:[
        'Apple', 'Banana', 'Cherry'
      ]
    }
  },
  computed: {
    hasFruit(){
      return this.fruits.length > 0
    },
    reverseFruits(){
      return this.fruits.map(fruit => {
        //Apple => split ['A','p','p','l','e'] => elppA
        return fruit.split('').reverse().join('')
      }
      )
    }
  }
}
</script>
반응형