What is nextTick or what does it do in VueJs

2019-02-02 03:09发布

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?

标签: vue.js vuejs2
3条回答
SAY GOODBYE
2楼-- · 2019-02-02 03:53

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 setTimeoutrelinquishes 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 used setTimeout, your callback would get called only now.

You can visualize this behavior by creating a small component like the following:

<template>
  <div class="hello">
    {{ msg }}
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
        msg: 'One'
    }
  },
  mounted() {
      this.msg = 'Two';

      this.$nextTick(() => {
          this.msg = 'Three';
      });
  }
}
</script>

Run your local server. You will see the message Three being displayed.

Now, replace your this.$nextTick with setTimeout

setTimeout(() => {
    this.msg = 'Three';
}, 0);

Reload the browser. You will see Two, before you see Three.

Check this fiddle to see it live

That's because, Vue updated the DOM to Two, gave control to the browser. Browser displayed Two. Then, called your callback. Vue updated the DOM to Three. Which the browser displayed again.

With nextTick. Vue udpated the DOM to Two. Called your callback. Vue updated the DOM to Three. Then gave control to the browser. And, the browser displayed Three.

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.

查看更多
虎瘦雄心在
3楼-- · 2019-02-02 03:54

To make Pranshat's answer about the difference between using nextTick and setTimeout, more explicit, I have forked his fiddle: here

mounted() {    
  this.one = "One";

  setTimeout(() => {
    this.two = "Two"
  }, 0);

  //this.$nextTick(()=>{
  //this.two = "Two"
  //})}

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.

查看更多
劫难
4楼-- · 2019-02-02 04:08

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).

// modify data
vm.msg = 'Hello'
// DOM not updated yet
Vue.nextTick(function () {
  // this function is called when vue has re-rendered the component.
})

// usage as a promise (2.1.0+, see note below)
Vue.nextTick()
  .then(function () {
      // this function is called when vue has re-rendered the component.
})

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.

查看更多
登录 后发表回答