{{#if currentUser}} with icon inside still rendered when user logs out

十年热恋 提交于 2021-01-27 19:52:37

问题


Thank you for reading my message, here is my problem :

I'm using a navbar made with Bulma in which I display either login/register buttons or a profile picture icon from FontAwesome using {{#if currentUser}} as shown below ("S'inscrire" and "Se connecter" means register and login in French) :

<div class="navbar-end">
    <div class="navbar-item">
        <div class="buttons">
            {{#if currentUser}}
                <!-- Displayed only when the user is logged in-->
                <i class="fas fa-user icon is-large" id="userProfile"></i>
            {{else}}
                <!-- Displayed when the user is NOT logged in-->
                <a class="button is-primary register">
                    <strong>S'inscrire</strong>
                </a>
                <a class="button is-light login">Se connecter</a>
            {{/if}}
        </div>
    </div>
</div>

When the user is not logged in it only show the buttons :

And when he logs in I have only the icon :

My problem is that when user logs out, I got both user icon and buttons :

and if I log in and out multiple times I get multiple icons. Looks like it's a problem with FontAwesome icon, I've tried with a normal image instead of the icon and it behaved normally, but I need to use icons inside this type of statement in some place of my app.

Thanks in advance !


回答1:


This is because font awesome 5 replaces the <i> tag with an svg which will break your code reactivity if it's not wrapped inside another flow element. Try wrapping the <i> with a <span> in order to keep the reactivity going:

<span>
  <i class="fas fa-user icon is-large" id="userProfile"></i>
</span>

You could also make this a short stateless template:

<template name="icon">
  <span>
    <i class="fas fa-{{name}} {{#if large}}is-large{{/if}}" id="{{id}}"></i>
  </span>
</template>

and call it like

{{>icon name="user" large=true id="userProfile"}}


来源:https://stackoverflow.com/questions/60639157/if-currentuser-with-icon-inside-still-rendered-when-user-logs-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!