[Vuejs]-VueJS – v-if and v-show don't work properly

0👍

You should run this piece of code in the mounted() callback of your component.

To me, it looks like you’re running it when the script has finished loading, which is not necessarily when the DOM is fully built and definitely not when Vue has finished rendering its components.

Working with v-if while using an external library is not a good idea anyway, you’re much better off initializing your view in the mounted() callback and then watching your chartShown variable like so:

{
  ...,
  watch: {
    chartShown(nv) {
      if (nv) {
        // setup chart
      } else {
        // remove chart
      }
    }
  },
  ...
}

Leave a comment