Answer the question
In order to leave comments, you need to log in
How to update the DOM inside a method before it completes?
There was a problem with turning on the loader before further long calculations (long cycle, data loading, sleep, etc.).
There is a flag searching
that is responsible for switching classes. The problem is that the classes are switched only after the execution of the search
. I checked this by removing the third line, the loader turned on after receiving the data, at the time of their rendering from the docs
.
methods: {
search: function () {
this.searching = true;
this.docs = getData(); // Долгие вычисления
this.searching = false; // (3)
}
},
<div class="column__body" :class="[{loader: searching}, {loader_fade: searching}]">
<doc-view v-for="doc in docs_on_opened_page" :doc="doc" :key="doc.id"></doc-view>
</div>
getData
, I "simulated" a long loading using a similar function:function sleep(ms) {
ms += new Date().getTime();
while (new Date() < ms) {}
}
methods: {
load: function () {
this.searching= true;
this.$nextTick(function () {
root.docs = getData();
root.searching= false;
})
}
},
sleep
with setTimeout
, although the question remained open.
Answer the question
In order to leave comments, you need to log in
methods: {
search: function () {
this.searching = true;
Vue.nextTick(()=> {
this.docs = getData();
this.searching = false;
})
}
},
Can be wrapped in Promise getData()
methods: {
search: function () {
this.searching = true;
getData().then(function (res) => {
this.docs = res;
this.searching = false;
});
}
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question