Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback for v-show in vue.js

Is there a callback method like on-shown and on-show using vue.js?

I'm using v-show="my-condition" on a div element. But inside are some charts.js charts, which cannot render unless visible.

Anyone know how can render the chart.js only when the parent is made visible? They are inside selectable tabs so it fires potentially multiple times.

I'm using Vue.js and vue-strap.

like image 433
Douglas Rosebank Avatar asked Jan 02 '16 02:01

Douglas Rosebank


People also ask

What does V show do in Vue?

The v-show directive is a Vue. js directive used to toggle the display CSS property of a element with our data via inline styles. If the data is true it will make it visible else it will make it invisible.

What does V bind do in Vue?

The v-bind directive is a Vuejs directive used to bind one or more attributes, or a component prop to an element. If that attribute is binded to our data defined in Vuejs instance then dynamically changes can be observed as data changes.

How do you show and hide in Vue?

Vue gives you a bunch of good ways to hide the element on the screen. When using v-if="false" the element isn't rendered at all in the DOM. When using v-show="false" the element is rendered in the DOM, however, Vue applies the inline style display: none that hides the element completely.

How do I watch data changes on Vue instance?

Using watchers in Vue # vue file we can watch for changes in data or props by using watch . For example, the below code will watch for a change in the data element pageData , and run a function according to the value it is changed to.


2 Answers

Check out this answer - using nextTick() worked for me in a similar situation. In a nutshell:

new Vue({
 ...

  data: {
    myCondition: false
  },

  watch: {
    myCondition: function () {
      if (this.myCondition) {
        this.$nextTick(function() {
          // show chart
        });
      } else {
        // hide chart
      }
    }
  },

  ...
})
like image 87
htompkins Avatar answered Sep 25 '22 09:09

htompkins


There is no event that you describe.

However, you could use a computed property like this:

new Vue({
  ...

  data: {
    myCondition: false
  },

  computed: {
    showChart: function () {
      if (this.myCondition) {
        // show chart
      } else {
        // hide chart
      }

      return this.myCondition
    }
  },

  ...
})


<div v-show="showChart"></div>

https://jsfiddle.net/xhfo24qx/

like image 32
Pantelis Peslis Avatar answered Sep 25 '22 09:09

Pantelis Peslis