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
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
.
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>