Home > Net >  Display FontAwesome icon in Vue 3 with JavaScript array
Display FontAwesome icon in Vue 3 with JavaScript array

Time:11-08

I am trying to display FontAwesomeicon

<font-awesome-icon icon="fa-solid fa-shop" />

with JavaScript array to be dynamic but it's not working it displays as text

<div  v-for="(item,index) in servicesArr" :key="index">
  <div >{{item.icon}}</div> <!--but its display as text not as icon svg-->
  <div >{{item.title}}</div>
  <p >{{item.description}}</p>
</div>
<script>
export default {
  data() {
    return {
      servicesArr: [
        {
          icon: `<font-awesome-icon icon="fa-solid fa-shop" />`,
          title: "E-commerce",
        }
      ]
    }
  }
}
</script>

CodePudding user response:

Isn't something like this conceivable in your case?

<template>
  <div  v-for="item in servicesArr" :key="item.icon">
    <div >
      <font-awesome-icon :icon="item.icon" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      servicesArr: [
        {
          icon: "fa-solid fa-shop",
          title: "E-commerce",
        }
      ]
    }
  }
}
</script>

Even if v-html exists, it's usually not the way to go.
Especially in such a simple use-case where dynamic props are totally fine.

Overall, I still recommend that solution for overall ease of use flexibility.

  • Related