Imagine having a Vue component stored in a .vue file with a data member called isLoading: false
. The template includes:
<div v-show="isLoading" id="hey" ref="hey">Loading...</div>
<button @click="loadIt()">Load it</button>
Along with a method:
loadIt() {
this.isLoading = true
this.$nextTick(() => {
console.log(this.$refs.hey)
// ...other work here that causes other DOM changes
this.isLoading = false
})
}
(The term "Loading" refers to loading from the local memory, not an external request. The goal is to display a simple "loading" message instantly while the potential 0.2-0.5 second DOM modifications are underway.)
The expectation was that the $nextTick
function would update both the virtual and actual DOM. Despite the console log indicating that the item was displayed (removing the display: none
style), the "Loading..." indicator never appears in Chrome or Firefox. The brief pause occurs, followed by the subsequent DOM changes without displaying the Loading indicator.
If opting for setTimeout
instead of $nextTick
, the loading indicator becomes visible, but only when the additional tasks run slowly enough. When there is a delay of several tenths of a second, the loading indicator remains hidden. The desired outcome is for it to show promptly upon clicking for a more responsive user interface.