3

I am having a problem updating my shown class when the data changes.

I have a servers array that calls to get the server status every 10 seconds. If the data changes, the data changes, but the class doesn't

The part that isn't changing is showing the font-awesome icon based on the status 'fas fa-exclamation-triangle critical' : 'fas fa-check ok'">

The text does change {{server.status}} just not the font-awesome class in the if statement.

Any ideas on what I need to change to get it to show correctly?

<tr v-for="server in servers">
    <td>
      {{server.name}}
      <a v-bind:href="server.url" target="_blank">{{server.url}}</a>
    </td>
    <td style="min-width: 125px">
      <i :class="server.status === 'CRITICAL' ? 'fas fa-exclamation-triangle critical' : 'fas fa-check ok'"></i>
      {{server.status}}
    </td>
    <td>{{server.revision}}</td>
    <td>{{server.notify}}</td>
    <td>{{server.count}}</td>

  </tr>

    <script>
  import axios from 'axios'

  export default {
    name: 'ServerMonitor',
    data() {
      return {
        servers: []
      }
    },
    created() {
      this.fetchData();
    },
    mounted: function () {
      setInterval(function () {
        this.fetchData();
      }.bind(this), 10000)
    },
    methods: {
      fetchData() {
        axios.get('https://SERVER/serverinfo')
          .then((resp) => {
            this.servers = resp.data[0].servers;
          })
          .catch((err) => {
            console.log(err);
          })
      }
    }
  }
</script>

Also I have tried it without the :class like this:

<i v-if="server.status === 'CRITICAL'" class="fas fa-exclamation-triangle critical"></i>
<i v-if="server.status === 'OK'" class="fas fa-check ok"></i>
7
  • Are you sure that server.status ever gets equal to 'CRITICAL'? Have you checked it outside the template? Commented Feb 9, 2018 at 22:51
  • Yes I have a mixture of 'CRITICAL' and 'OK' Responses. And they initially work just great. The problem is when it makes the call again 10 seconds later and it changes. The text changes but the font-awesome image doesn't Commented Feb 9, 2018 at 22:53
  • 2
    You ought to have a :key Commented Feb 9, 2018 at 23:02
  • 1
    It seems to be the problem connected to the font awesome. I just tested it and it seems like <i class="something"></i> renders as svg on page, so it's not dependant on the component data. I'm not sure how to avoid that yet. Commented Feb 9, 2018 at 23:25
  • 2
    Yes, apparently you have to use a different Font Awesome lib for Vue as they recommend in their docs: fontawesome.com/how-to-use/js-component-packages Commented Feb 9, 2018 at 23:30

2 Answers 2

6

Vue's v-bind:class takes an object or an Array and not a string, which is probably your issue.

   <td style="min-width: 125px">
      <i :class="['fas', server.status === 'CRITICAL' ? 'fa-exclamation-triangle critical' : 'fa-check ok']"></i>
      {{server.status}}
   </td>

Updating my answer based on comments below:

You need to use the font-awesome Vue component. What's happening is that FontAwesome is converting the <i> icons to SVG once, and doesn't rerender them at any future point.

Edit 2 Alternatively you can use the v4 upgrade shim:

<script defer src="https://use.fontawesome.com/releases/v5.0.6/js/v4-shims.js"></script>

https://jsfiddle.net/6tfqp4nb/12/

Sign up to request clarification or add additional context in comments.

8 Comments

This is definitely not the case, you can use strings for class-binding: jsfiddle.net/oniondomes/6tfqp4nb/1
I have also tried doing it outside the v-bind:class like this and have the same problem <i v-if="server.status === 'CRITICAL'" class="fas fa-exclamation-triangle critical"></i> <i v-if="server.status === 'OK'" class="fas fa-check ok"></i>
Are you using vue devtools to analyse the data object in your component?
You may be experiencing this: vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats because you are setting your data to an array of objects, it may not be reactive to future updates
@jostrander check this out: jsfiddle.net/oniondomes/fjh9ugex. This clearly happens to be connected with font awesome
|
0

If you are using font-awesome in js way, you can try this:

FontAwesomeConfig = { autoReplaceSvg: 'nest' }

doc: https://fontawesome.com/how-to-use/svg-with-js#auto-replace-svg-nest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.