Home  >  Q&A  >  body text

Best way to change FontAwsome star class general to fixed

I want to change the 5 star category of a FontAwesome icon from regular to solid based on a numeric variable level that changes from 0 to 5

<template>
    <div id="five-stars">
      <font-awesome-icon icon="fa-solid fa-star" size="6x"/>
      <font-awesome-icon icon="fa-regular fa-star" size="6x"/>
      <font-awesome-icon icon="fa-regular fa-star" size="6x"/>
      <font-awesome-icon icon="fa-regular fa-star" size="6x"/>
      <font-awesome-icon icon="fa-regular fa-star" size="6x"/>
    </div>
</template>

<script>
export default {
  name: "ThreeScene",
  data() {
    return {
      level: 1
    };
  }
}

Can you tell me how to do this without repeating

five times? Thanks in advance.

P粉998100648P粉998100648372 days ago549

reply all(2)I'll reply

  • P粉733166744

    P粉7331667442023-09-14 09:53:03

    Use v-for loop

    <template>
        <div id="five-stars">
          <font-awesome-icon 
            v-for="level in 5" 
            :key="level"
            :icon="`${level} fa-star" 
            size="6x"
          />
        </div>
    </template>
    
    <script setup>
    import { ref } from 'vue'
    
    const data = ref([
      'fa-solid',
      'fa-regular',
      'fa-solid',
      'fa-regular',
      'fa-solid'
    ])
    
    </script>

    Please note that the variable level will start with the value 1, not 0.

    reply
    0
  • P粉135292805

    P粉1352928052023-09-14 00:58:58

    fa-${i <= 级别? 'solid' : 'regular'} Help you:

    <template>
      <div id="five-stars">
        <font-awesome-icon v-for="i in 5" :key="i" :icon="`fa-${i <= level ? 'solid' : 'regular'} fa-star`" size="6x"/>
      </div>
    </template>
    
    <script>
    export default {
      name: "ThreeScene",
      data() {
        return {
          level: 1
        };
      }
    }
    </script>

    reply
    0
  • Cancelreply