最佳方法将Font Awesome星星类从”regular”更改为”solid”。

huangapple go评论66阅读模式
英文:

Best way to change FontAwsome stars class regular to solid

问题

要根据一个名为level的数字变量从05来将FontAwesome Icons的5个星星的类从regular更改为solid,您可以使用条件语句和循环来避免重复<div>五次。以下是示例代码:

<template>
    <div id="five-stars">
        <font-awesome-icon
            v-for="star in 5"
            :key="star"
            :icon="star <= level ? 'fa-solid fa-star' : 'fa-regular fa-star'"
            size="6x"
        />
    </div>
</template>

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

这段代码使用了v-for循环来生成五个星星的图标,根据level的值决定是使用'solid'还是'regular'类。这样,您不需要重复<div>五次,而是在循环内动态生成图标。

英文:

I want to change the class of 5 stars of FontAwesome Icons from regular to solid according to a numeric variable levelthat changes from 0 to 5

&lt;template&gt;
    &lt;div id=&quot;five-stars&quot;&gt;
      &lt;font-awesome-icon icon=&quot;fa-solid fa-star&quot; size=&quot;6x&quot;/&gt;
      &lt;font-awesome-icon icon=&quot;fa-regular fa-star&quot; size=&quot;6x&quot;/&gt;
      &lt;font-awesome-icon icon=&quot;fa-regular fa-star&quot; size=&quot;6x&quot;/&gt;
      &lt;font-awesome-icon icon=&quot;fa-regular fa-star&quot; size=&quot;6x&quot;/&gt;
      &lt;font-awesome-icon icon=&quot;fa-regular fa-star&quot; size=&quot;6x&quot;/&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  name: &quot;ThreeScene&quot;,
  data() {
    return {
      level: 1
    };
  }
}

Can you please tell me how can I do that without repeating the &lt;div&gt; five times? thanks in advance.

答案1

得分: 1

fa-${i &lt;= level ? &#39;solid&#39; : &#39;regular&#39;} 帮助你:

<template>
  <div id="five-stars">
    <font-awesome-icon v-for="i in 5" :key="i" :icon="`fa-${i &lt;= level ? &#39;solid&#39; : &#39;regular&#39;} fa-star`" size="6x"/>
  </div>
</template>

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

fa-${i &lt;= level ? &#39;solid&#39; : &#39;regular&#39;} help you :

&lt;template&gt;
  &lt;div id=&quot;five-stars&quot;&gt;
    &lt;font-awesome-icon v-for=&quot;i in 5&quot; :key=&quot;i&quot; :icon=&quot;`fa-${i &lt;= level ? &#39;solid&#39; : &#39;regular&#39;} fa-star`&quot; size=&quot;6x&quot;/&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  name: &quot;ThreeScene&quot;,
  data() {
    return {
      level: 1
    };
  }
}
&lt;/script&gt;

答案2

得分: 0

使用 v-for 循环

<template>
    <div id="five-stars">
        <font-awesome-icon 
            v-for="level in 5"
            :key="level"
            :icon="`${level} fa-star`"
            size="6x"
        ></font-awesome-icon>
    </div>
</template>
</html>

<script setup>
import { ref } from 'vue';

const data = ref([
  'fa-solid',
  'fa-regular',
  'fa-solid',
  'fa-regular',
  'fa-solid'
]);
</script>

请注意,变量 level 从值 1 开始,而不是 0

英文:

Use a v-for loop

&lt;template&gt;
    &lt;div id=&quot;five-stars&quot;&gt;
      &lt;font-awesome-icon 
        v-for=&quot;level in 5&quot; 
        :key=&quot;level&quot;
        :icon=&quot;`${level} fa-star&quot; 
        size=&quot;6x&quot;
      /&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { ref } from &#39;vue&#39;

const data = ref([
  &#39;fa-solid&#39;,
  &#39;fa-regular&#39;,
  &#39;fa-solid&#39;,
  &#39;fa-regular&#39;,
  &#39;fa-solid&#39;
])

&lt;/script&gt;

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

huangapple
  • 本文由 发表于 2023年2月23日 23:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定