I read the docs, but I can't understand it. I know what data, computed, watch, methods do but what is nextTick()
use for in vuejs?
相关问题
- Axios OPTIONS instead of POST Request. Express Res
- Vue.js - set slot content programmatically
- Simple vue app doesn't show anything
- Getting Uncaught TypeError: …default is not a cons
- Vue.js computed property loses its reactivity when
相关文章
- Best way to dynamically change theme of my Vue.js
- vue-router how to persist navbar?
- How to Properly Use Vue Router beforeRouteEnter or
- Difference between nameFunction() {} and nameFunct
- Vue - best way to reuse methods
- setTimeout() not working called from vueJS method
- How to unbind an array copy in Vue.js
- Vue Cli 3 Local fonts not loading
nextTick allows you to do something after you have changed the data and VueJS has updated the DOM based on your data change, but before the browser has rendered those changed on the page.
Normally, devs use native JavaScript function setTimeout to achieve similar behavior. But, using
setTimeout
relinquishes control over to the browser before it gives control back to you via your callback.Let's say, you changed some data. Vue updates DOM based on the data. Mind you the DOM changes are not yet rendered to the screen by the browser. If you used
nextTick
, your callback gets called now. Then, browser updates the page. If you usedsetTimeout
, your callback would get called only now.You can visualize this behavior by creating a small component like the following:
Run your local server. You will see the message
Three
being displayed.Now, replace your
this.$nextTick
withsetTimeout
Reload the browser. You will see
Two
, before you seeThree
.Check this fiddle to see it live
That's because, Vue updated the DOM to
Two
, gave control to the browser. Browser displayedTwo
. Then, called your callback. Vue updated the DOM toThree
. Which the browser displayed again.With
nextTick
. Vue udpated the DOM toTwo
. Called your callback. Vue updated the DOM toThree
. Then gave control to the browser. And, the browser displayedThree
.Hope it was clear.
To understand how Vue implements this, you need to understand the concept of Event Loop and microtasks.
Once you have those concepts clear(er), check the source code for nextTick.
To make Pranshat's answer about the difference between using nextTick and setTimeout, more explicit, I have forked his fiddle: here
You can see in the fiddle that when using setTimeOut, the initial data flashes very briefly once the component gets mounted before adapting the change. Whereas, when using nextTick, the data is hijacked, changed, before rendering to the browser. So, the browser shows the updated data without even any knowledge of the old. Hope that clears the two concepts in one swoop.
Next Tick basically allows you to run some code, after the vue has re-rendered the component when you have made some changes to the a reactive property (data).
From the Vue.js Documentation:
Defer the callback to be executed after the next DOM update cycle. Use it immediately after you’ve changed some data to wait for the DOM update.
Read more about it, here.