What is nextTick or what does it do in VueJs

后端 未结 4 810
青春惊慌失措
青春惊慌失措 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:00

    The content has been taken from By Adrià Fontcuberta

    Vue documentation says:

    Vue.nextTick( [callback, context] )

    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.

    Hmm..., if it feels intimidating at first, do not worry I will try to explain it as simple as possible. But first there are 2 things you should know:

    1. Its usage is uncommon. Like one of those silver magic cards. I have written several Vue apps and ran into nextTick() once or twice.

    2. It’s easier to understand once you’ve seen some real use cases. After you get the idea, the fear will go away, and you will have a handy tool under your belt.

    Let’s go for it, then.

    Understanding $nextTick

    We are programmers, aren’t we? We’ll use our beloved divide and conquer approach to try to translate the description of .nextTick() bit by bit. It starts with:

    Defer the callback

    Ok, now we know it accepts a callback. So it looks like this:

    Vue.nextTick(function () {
      // do something cool
    });
    

    Great. This callback is deferred (this is how millenials say delayed) until…

    the next DOM update cycle.

    Okay. We know that Vue performs DOM updates asynchronously. It features a way of keeping these updates “stored” until it needs to apply them. It creates a queue of updates and flushes it when needed. Then, the DOM is “patched” and updated to its latest version.

    What?

    Let me try again: Imagine your component does something really essential and smart like this.potatoAmount = 3. Vue won’t re-render the component (and thus the DOM) automatically. It’ll queue up the required modification. Then, in the next “tick” (as in a clock), the queue is flushed, and the update is applied. Tada!

    Okay! So we know that we can use nextTick() to pass a callback function that is executed right after the data is set and the DOM has updated.

    As I said earlier… not that often. The “data flow” approach that drives Vue, React, and the other one from Google, which I won't mention, makes it unnecessary most of the time. Yet, sometimes we need to wait for some elements to appear/disappear/be modified in the DOM. This is when nextTick comes in handy.

    Use it immediately after you’ve changed some data to wait for the DOM update.

    Exactly! This is the last piece of definition that Vue docs provided to us. Inside our callback, the DOM has been updated so we can interact with the “most updated” version of it.

    Prove it

    Okay, okay. See the console, and you’ll see that the value of our data is updated only inside the callback of nextTick:

    const example = Vue.component('example', {
      template: '

    {{ message }}

    ', data: function () { return { message: 'not updated' } }, mounted () { this.message = 'updated' console.log( 'outside nextTick callback:', this.$el.textContent ) // => 'not updated' this.$nextTick(() => { console.log( 'inside nextTick callback:', this.$el.textContent ) // => 'not updated' }) } }) new Vue({ el: '#app', render: h => h(example) })
    
    

    A use case

    Let’s try to define some useful use case for nextTick.

    Imagine that you need to perform some action when a component is mounted. BUT! not only the component. You also need to wait until all its children are mounted and available in the DOM. Damn! Our mounted hook doesn’t guarantee that the whole component tree renders.

    If only we had a tool to wait for the next DOM update cycle…

    Hahaa:

    mounted() {
      this.$nextTick(() => {
        // The whole view is rendered, so I can safely access or query
        // the DOM. ¯\_(ツ)_/¯
      })
    }
    

    In a nutshell

    So: nextTick is a comfortable way to execute a function after the data has been set, and the DOM has been updated.

    You need to wait for the DOM, maybe because you need to perform some transformation or you need to wait for an external library to load its stuff? Then use nextTick.

    Some people also use nextTick in their unit tests as a way to ensure that data has been updated. This way, they can test the “updated version” of the component.

    Vue.nextTick() or vm.$nextTick()?

    Don’t worry. Both are (almost) the same. Vue.nextTick() refers to the global API method, while vm.$nextTick() is an instance method. The only difference is that vm.$nextTick doesn’t accept a context as the second parameter. It is always bound to this (also known as the instance itself).

    A last piece of coolness

    Notice that nextTick returns a Promise, so we can go full-cool with async/await and improve the example:

    async mounted () {
        this.message = 'updated'
        console.log(this.$el.textContent) // 'not updated'
        await this.$nextTick()
        console.log(this.$el.textContent) // 'updated'
    }
    

提交回复
热议问题