Font Awesome not updating properly with vue
Asked Answered
O

2

4

I'm trying to make a clickable "star" icon using font-awesome with bulma, switching between regular and solid styles (fas and far) in Vue, to achieve this I have the following component:

<template>
    <span v-if="isStarred" class="icon starred config-icon clickable-text" @click="unstar">
        <i class="far fa-star" title="Unstar Ranking"/>
    </span>
    <span v-else class="icon unstarred config-icon clickable-text" @click="star">
        <i class="fas fa-star" title="Star Ranking"/>
    </span>
</template>

The value isStarred is being updated properly and the span elements are updating accordingly. However the i element with the icon doesn't update until I fully reload the page.

I was able to make this work with v-show instead of v-if but I don't understand why this wouldn't work.

Opportune answered 8/5, 2018 at 22:6 Comment(0)
E
13

Vue tries to render elements as efficiently as possible, often re-using them instead of rendering from scratch. This isn’t always desirable though, so Vue offers a way for you to say, “These two elements are completely separate - don’t re-use them.” Add a key attribute with unique values:

<i class="far fa-star" title="Unstar Ranking" key="unstared"/>
...
<i class="fas fa-star" title="Star Ranking" key="stared"/>

Now those i elements will be rendered from scratch each time you toggle.

You can also update your classes with class binding:

<span v-if="isStarred" v-bind:class="{starred: isStarred, unstarred: !isStarred}" class="icon config-icon clickable-text" @click="toggleStar">
    <i v-bind:class="{far: isStarred, fas: !isStarred}" class="fa-star" v-bind:title="title"/>
</span>
Eggbeater answered 8/5, 2018 at 22:50 Comment(4)
Thanks, the key is working fine. However class binding didn't work (is the first thing I actually tried before using separate components)Opportune
The example given using key on the font awesome icons didn't work for me, however adding a key to the encloding button the icon was a part of did. That was a stumper, thanks Jakub.Chit
Had the same problem with ionicons and this fixed it perfectly. Thanks!Acarus
For me this didn't work. But setting the key on the PARENT element worked fine.Hittel
C
11

I switched the [fontawesome/js/all.js] to [fontawesome/css/all.css], and it solved the problem. I guess the fontawesome js is rendered after the vue.

Cable answered 15/3, 2019 at 2:7 Comment(1)
I was importing both. Just commenting import '@fortawesome/fontawesome-free/js/all.js' solved it to me too.Dogtired

© 2022 - 2024 — McMap. All rights reserved.