Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display FontAwesome icon in Vue 3 with JavaScript array

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Isn’t something like this conceivable in your case?

<template>
  <div class="aboutme-card" v-for="item in servicesArr" :key="item.icon">
    <div class="service-card-icon">
      <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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading