What is nextTick or what does it do in VueJs

后端 未结 4 819
青春惊慌失措
青春惊慌失措 2020-12-07 11:38

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?

4条回答
  •  清歌不尽
    2020-12-07 12:06

    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.

提交回复
热议问题