Keep rendering the component even when the leave transition is in progress

I am facing an issue with a component whose value keeps changing. When the component is hidden using a transition, it stops updating the value, which is not the desired behavior.

To see the problem in action, visit this fiddle or refer to this code snippet:

var example = new Vue({
  el: '#example',
  data: {
    visible: true,
    counter: 0
  },
  created: function() {
    setInterval(function() {
      this.counter++;
    }.bind(this), 200);
  }
});
.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.fade-enter-active,
.fade-leave-active {
  transition: 2s ease;
}

[v-cloak] {
  display: none;
}
<script src="https://vuejs.org/js/vue.min.js"></script>

<div id="example" v-cloak>
  <p>Value: {{ counter }}</p>

  <transition name="fade">
    <p v-if="visible">Transition: {{ counter }}</p>
  </transition>

  <button @click="visible = !visible">
    Transition!
  </button>
</div>

Upon clicking the button, you will notice that the fading paragraph stops updating at the current counter value while the other one keeps updating. How can I resolve this? I want the fading paragraph to only stop updating when it is completely hidden after the transition has completed.

Answer №1

In addition to what @Sandwell and @Nope have already stated,

After the transition has finished, you have the option to remove the element.

Link to example

<p v-show="visible" v-if="exists" @transitionend="handleTransitionend">

Furthermore,

new Vue({
    data: {
        // ...
        exists: true
    },
    methods: {
        handleTransitionend() {
            this.exists = false;
      }
    }
});

UPDATE:

@HristiyanDodov has enhanced the solution based on input from @Sandwell and myself, utilizing a transition hook @before-enter and @after-leave.

Here is the complete solution: Complete Solution Link

Answer №2

The reason for the freezing is that the v-if condition triggers the destroy event, causing the component to no longer exist and therefore cannot be bound.

To avoid this issue, it is recommended to use v-show instead of v-if

See implementation here

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

Tips for Ensuring Proper Scroll Functionality

$(document).scroll(function(e) { if($(document).scrollTop() >=300) { $(document).scrollTop(0); } }); I need help implementing a return false/return true or bind/unbind feature for this function. Can you provide guidance on how to do t ...

What is the most effective method for exchanging variables between programs or threads?

I currently have a program that executes an algorithm processing real-time data. Once per hour, the algorithm's parameters are optimized based on new historical data. Currently, this optimization process is running in a single thread, pausing the rea ...

What is the best way to integrate model classes within an Angular module?

I have a few classes that I want to keep as plain bean/DTO classes. They are not meant to be display @component classes, @Pipe classes, or @Directive classes (at least, that's what I believe!). I am trying to bundle them into a module so that they ca ...

How can we enable Material UI tooltip based on certain conditions?

In my React component utilizing Material UI, I have the code snippet below: const MyButton = ({ warningText }) => ( <Tooltip title={warningText}> <Button>Do action</Button> </Tooltip> ) At present, an empty tool ...

The texture fails to display upon loading the .dae model

I'm attempting to load a .dae model in three.js, but it seems like the texture is not loading. Here is my code snippet: <script src="js/three.js"></script> <script src="js/Animation.js"></script> <script src="js/An ...

Steps to inject HTML content via id using a technology such as AngularJS' ng-include

My current project involves using AngularJS to compile a directive into the main page. This directive contains a series of buttons that dynamically change based on its internal state. I am interested in relocating these buttons to a menu-bar within the pag ...

What is the best way to generate a hierarchical bar graph using d3.js?

I was attempting to modify Mike Bostock's Sortable Bar Chart by removing the transition property and sorting the bars based on their length, similar to how it is displayed in this example, without any interactivity. <!DOCTYPE html> <meta cha ...

Guide on adjusting the darkness of a MaterialUI Avatar component image and adding text to it

I'm currently working with the Avatar component from Material UI along with Tailwind CSS. I found that by simply adding the image URL to the src, it displays the avatar successfully. <Avatar alt="Cindy Baker" src="https://mui.com/sta ...

Extracting Values from a jQuery Array Object

Good day everyone! Here is the code I am currently working with: var items = []; $(xml).find("Placemark").each(function () { var tmp_latLng = $(this).find("coordinates").text(); tmp_latLng = tmp_latLng.split(","); items.push({ name: ...

Having difficulty transferring user input file from Vue.js to Symfony PHP

Sample text herePlaceholder image descriptionI'm encountering an issue with sending an image file from the frontend (Vue.js) to the backend (PHP Symfony). I have set up a form where users can upload an image file, and now I need to send that file to t ...

What is the best method for making certain rows uneditable in jqgrid?

Can rows be set as uneditable in jqgrid after the first edit is done? I attempted to add a class called not-editable-row but it did not work. This is how I currently make all rows editable: onSelectRow: function(id){ if(id && id!==lastsel){ g ...

How can I apply a blurred effect to the background image of a jumbotron in Bootstrap-vue without affecting the clarity of the text overlay

I need help figuring out how to blur the background image of my jumbotron without blurring the text on top. <b-container fluid class="text-center"> <div> <b-jumbotron class="jumbotron"> <p style=& ...

v-for loop doesn't iterate over imported array

I am facing an issue with importing data from a file named clients.js into another file called clients.vue. My goal is to display the imported data in a table within the clients.vue file, but I am unable to access it after importing. Interestingly, if I c ...

The error message "gulp error - _.flattenDeep is not a function when using gulp.watch" is indicating

Whenever I run a watch task, why am I encountering the TypeError: _.flattenDeep is not a function error? Below is the content of my gulpfile.js : var gulp = require('gulp'); var sass = require('gulp-sass'); var sourcemaps = require(&a ...

Using data-binding to loop through multiple arrays with the {foreach: ...} directive

Can the data-bind: foreach be used with multiple arrays simultaneously? For example: <div data-bind="foreach: arrayone, arraytwo"> //do stuff </div> If it is possible, what is the proper syntax to achieve this? Or is there a different ...

Cannot update VUEjs array following the splice operation

My array is only updating in the console and not in the DOM. I've already tried using :key but it's still not working. Here is my code: <div style="margin-top: 5px;" v-for="(file, index) in editedItem.file_name" ...

The jQuery remove function will only take effect on the second click following an AJAX request

I'm facing an issue with my jQuery code where two divs contain lists of links, triggering AJAX calls to append JSON data to a separate div. Upon clicking a link, the corresponding link div should hide. There's also a third div with the class "pan ...

How can we refresh an updatePanel on the main page from a popup using ASP.Net?

Looking for assistance, thank you in advance. I have two aspx pages - the first one is the main page with an updatepanel where I call the second page as a popup. I am looking for a way to update the updatepanel on the main page from the popup. Thank you f ...

Accessing the value of a hidden field within an ng-repeat loop when binding to an HTML table

I am working on a project where I have an HTML table with data being bound from AngularJS. The structure looks something like this: <tr ng-repeat="item in List| filter: { MachineType: 'Physical' }"> <td>{{item.MachineType}}< ...

What is the best way to establish the starting property state for CSS Animation?

I'm looking to create an animation that involves the following steps: Initial: elementA starts with opacity set at 0; When hovering over containerA, which contains elementA: elementA transitions from position (-5px, 5px) to (0px, 0px); elementA ch ...