The icons from FontAwesome in Vue do not update when computed

I am seeking a way to dynamically change the header icon based on a conversation property.

<a class="navbar-item" :title="$t('header.lock')" @click="makePrivate">
    <i class="fas" :class="getLockClass"></i>
</a>

These are the computed properties in use:

isConversationPrivate() {
    return !(Object.keys(this.conversation).length === 0 || this.conversation.user_id === null);
},
getLockClass() {
    console.log(this.isConversationPrivate);
    return this.isConversationPrivate ? 'fa-lock' : 'fa-unlock-alt';
}

Initially, when the page is loaded, the conversation is empty leading to the console log printing false (no existing conversation).

Upon making an Axios request to fetch a conversation by ID, and once it is retrieved and the method getLockClass runs again, the console will now show true. However, despite the change occurring, the class never gets applied. Why does this not work?

UPDATE:

I have replicated the issue in this example:

<html lang="fr">
<body>

<div id="app">
    <a @click="toggle">
        <i class="fas" :class="this.lock ? 'fa-lock':'fa-unlock'"></i>
    </a>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
<script>

    new Vue({
        el     : '#app',
        data   : {
            lock: true
        },
        methods: {
            toggle() {
                console.log("here");
                this.lock = !this.lock;
            }
        },
    });
</script>

</body>

</html>

Answer №1

The issue arises from the fact that you are utilizing a FontAwesome library, which will essentially comment out the <i> element and substitute it with an <svg>. You can verify this by inspecting the element in your browser. To address this problem, FontAwesome suggests using vue-fontawesome for better compatibility and other benefits: https://fontawesome.com/how-to-use/on-the-web/using-with/vuejs

An alternative approach would be to enclose the <i> within another element (such as a span):

<span v-show="lock"><i class="fas fa-lock"/></span>
<span v-show="!lock"><i class="fas fa-unlock"/></span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you explain the meaning of this PHP syntax and why is this variable showing NaN?

Looking for question marks on Google can be quite challenging. Can someone explain this process step by step? $page = isset($_POST['page'])?$_POST['page']:"0"; I believe it means to check if $_POST['page'] is set, use that ...

When using Promise.all(), it surprisingly gives back an array of Promise <Pending>'s that are undefined, even though other solutions produce successful results

Currently, I am in the process of developing a web application that enables users to access a dashboard displaying various items. Each item comprises sections labeled as a, b, and c, which are represented as complete (o) or incomplete (x) on the dashboard. ...

Implementing experimental decorators and type reconciliation in TypeScript - A step-by-step guide

My basic component includes the following code snippet: import * as React from 'react'; import { withRouter, RouteComponentProps } from 'react-router-dom'; export interface Props { }; @withRouter export default class Movies extends R ...

Having difficulty controlling the DOM using cheerio in a Node.js environment

I am currently working on a basic program using Node for the server side with cheerio. Here are the code snippets provided: Server Side: /** * Module dependencies. */ var express = require('express') , routes = require('./routes') ...

standalone visuals generated interactively with matplotlib

While I appreciate the plots generated by matplotlib and the ability to save them as SVG, there is a feature missing that I would like to see added... I am looking for a way to save the figure as an SVG file with embedded JavaScript code to add interactiv ...

Vue Server-Side Rendering does not retain the URL hash during redirection

Having an issue with Vue.js application and Server-side Rendering (SSR). For example, I have a page with URL anchors like this http://localhost/faq#question5 However, when running SSR, the hash part of the URL is lost. According to the documentation: r ...

How can I specify to a Vue application that it will be situated in a subdirectory and have the image paths updated accordingly?

I have my Vue app located in a subfolder accessible via the URL: domain.com/myapp/ Within my component template, I currently use: <img :src= "base_path + '/img/undraw_profile.svg'"> where base_path is included in an imported fil ...

Maintaining sequential order IDs for table rows even after removing records

I currently have a table structured as follows: <table> <tr> <td> <input type="hidden" name="help[0].id" /> </td> <td> <span class="tr-close">X</span> </tr> <tr ...

When the jQuery document is ready, it typically returns null, but the console can still access and display

I have encountered an issue while working on a solution within a CMS (EPiServer). When I utilize console.log to check my object, it displays a null value. $(document).ready(function () { console.log("$('.EPiRequester').html() =" + $('. ...

The code function appears to be malfunctioning within the Cordova platform

When working with Cordova, I encountered an issue where I needed to trigger a button event from a listener. In my app, I am loading a specific page . This page contains a button with the class name "btn", and I wanted to display an alert when that button i ...

Is there a method in AngularJS to automatically set app.comment to null if the point is not equal to 1 using front end logic?

Can we verify on the front end in AngularJS if app.point !=1 and app.comment==null? <td> <input type="number" max="5" min="1" ng-init="app.point=5" data-ng-model="app.point"> </td> < ...

Is the Vuex mutation properly formatted?

Is the mutation method correctly written to modify the initial state array? I'm uncertain about the last few lines of the mutation method. What am I missing, if anything? // Storing state: { flights: [ {trip_class: 0, number_of_change="1"}, ...

Posts created in Express using the node-postgres module are not being retrieved by queries in async routes

Running a basic query from an express route seems to be causing some issues for me: var router = require('express-promise-router')() const { Pool } = require('pg') const pool = new Pool({ user: 'user', password: 'pa ...

Angular popup window can't access DOM elements

Hey there, currently experimenting with an angular modal instance and running into a bit of a hurdle. When attempting to declare a querySelector in the modal controller, I'm encountering a console error stating 'Cannot read property 'queryS ...

Tips for modifying date format in Angular 8

My datepicker for a date column is displaying the incorrect date format after submission. I am looking to change this format to the correct one. I am working with bsConfig bootstrap in Angular 8, but I am unsure of how to modify the date format. The back ...

Create a custom JavaScript library by incorporating an external library into it as a bundle

As I work on developing a library, one of the dependencies I plan to use is fabricjs. Installing fabricjs involves specific libraries and versions that can be cumbersome for users. Despite successfully installing it in my project and using it, my concern l ...

The redirection from Azure AD SSO is not working properly following a logout

In my quest to integrate Azure AD login into a single-page application built with ReactJS, I utilized the MSAL React package. While successfully implementing the login functionality, I encountered an issue during logout where I found myself unable to redir ...

Implement a sequence of animations within a div using jQuery

My goal is to create an animation effect for each div element within a row when the user scrolls down to that specific point on the page. The layout consists of three <div> elements in the same row. The structure of the HTML code is as shown below: ...

Attempting to retrieve file using vue-resource corrupts the format of the .xls file

When trying to download a .xls file via API, the output file format is incorrect. The issue can be seen in the following image: Here is the code snippet that I am using: exportData: function() { let items_ids = []; this.$refs['vue-items-reports- ...

Hover over the image with some padding placed around it to see

I am struggling to figure out how to add padding to my image when hovering over it with Onmouseover. I have tried multiple solutions but none seem to be working. Here is the original code: <img src='/wp-content/uploads/2015/04/img-white.png' ...