美文网首页
nextTick和异步更新视图

nextTick和异步更新视图

作者: Raral | 来源:发表于2020-11-16 17:50 被阅读0次

    加深vue渲染页面原理记录

    1. 栗子

    先来了解一个例子关于vue中数据和DOM更新机制以及vm.nextTick作用。

    template

    <template>
        <div>
            <div ref="msg">{{msg}}</div>
            <button @click="changeMsg">点击</button>
        </div>
    </template>
    

    script

    export default {
        data() {
            return {
                msg:"default",
            }
        },
        methods: {
            changeMsg() {
                //把 nextTick放在前面结果也是一样。
                // this.$nextTick(() => {
                //     console.log("我是nextTick:" + this.$refs.msg.textContent)//改变最新的值
                // })
                this.msg =  new Date().getTime();
                console.log(this.msg)
                this.msg =  new Date().getTime();
                console.log(this.msg)
                this.msg =  new Date().getTime();
                console.log(this.msg)
                this.msg =  new Date().getTime();
                console.log(this.msg)// 改变最新的值
                console.log("我是外边:" + this.$refs.msg.textContent)//初始值
                setTimeout(() => {
                    console.log("我是setTimeout:" + this.$refs.msg.textContent)//改变最新的值
                }, 0)
                this.$nextTick(() => {
                    console.log("我是nextTick:" + this.$refs.msg.textContent)//改变最新的值
                })
            }
        },
    }
    

    点击按钮 控制台结果图


    nextTick.jpg

    通过图上可知:DOM更新是异步的,我们只能通过异步的手法让获取DOM值是最新改变的值。并且nextTick和传统的setTimeout有异曲同工之处。

    2. 什么是异步更新队列

    官方文档解释

    Vue 在更新 DOM 时是异步执行的。只要侦听到数据变化,Vue 将开启一个队列,并缓冲在同一事件循环中发生的所有数据变更。如果同一个 watcher 被多次触发,只会被推入到队列中一次。这种在缓冲时去除重复数据对于避免不必要的计算和 DOM 操作是非常重要的。然后,在下一个的事件循环“tick”中,Vue 刷新队列并执行实际 (已去重的) 工作。Vue 在内部对异步队列尝试使用原生的 Promise.then、MutationObserver 和 setImmediate,如果执行环境不支持,则会采用 setTimeout(fn, 0) 代替

    例如,当你设置 vm.someData = 'new value',该组件不会立即重新渲染。当刷新队列时,组件会在下一个事件循环“tick”中更新。多数情况我们不需要关心这个过程,但是如果你想基于更新后的 DOM 状态来做点什么,这就可能会有些棘手。虽然 Vue.js 通常鼓励开发人员使用“数据驱动”的方式思考,避免直接接触 DOM,但是有时我们必须要这么做。为了在数据变化之后等待 Vue 完成更新 DOM,可以在数据变化之后立即使用 Vue.nextTick(callback)。这样回调函数将在 DOM 更新完成后被调用

    注意:
    看了大堆文字说明还是懵逼的感觉,简单来说:这也和vue数据响应原理有关联的,异步更新队列意思,就当vue检测到data变化以后,由于watcher触发了,所以会被推入到队列中,这也是操作DOM的时候,是不会立即更新DOM的,所以用nextTick(),可以立即更新DOM,获取最新的DOM状态。
    要想理解官方文档的说明,我们必须先了解js中的事件循环机制和事件队列的概念可以借鉴
    Promise 解析和事件循环机制

    应用场景:
    在Vue生命周期的created()钩子函数进行的DOM操作一定要放在Vue.nextTick()的回调函数中。

    3. Vue 2.6.10 源码浅析

    /* @flow */
    /* globals MutationObserver */
    
    import { noop } from 'shared/util'
    import { handleError } from './error'
    import { isIE, isIOS, isNative } from './env'
    
    export let isUsingMicroTask = false
    
    const callbacks = []
    let pending = false
    
    function flushCallbacks () {
      pending = false
      const copies = callbacks.slice(0)
      callbacks.length = 0
      for (let i = 0; i < copies.length; i++) {
        copies[i]()
      }
    }
    
    // Here we have async deferring wrappers using microtasks.
    // In 2.5 we used (macro) tasks (in combination with microtasks).
    // However, it has subtle problems when state is changed right before repaint
    // (e.g. #6813, out-in transitions).
    // Also, using (macro) tasks in event handler would cause some weird behaviors
    // that cannot be circumvented (e.g. #7109, #7153, #7546, #7834, #8109).
    // So we now use microtasks everywhere, again.
    // A major drawback of this tradeoff is that there are some scenarios
    // where microtasks have too high a priority and fire in between supposedly
    // sequential events (e.g. #4521, #6690, which have workarounds)
    // or even between bubbling of the same event (#6566).
    let timerFunc
    
    // The nextTick behavior leverages the microtask queue, which can be accessed
    // via either native Promise.then or MutationObserver.
    // MutationObserver has wider support, however it is seriously bugged in
    // UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It
    // completely stops working after triggering a few times... so, if native
    // Promise is available, we will use it:
    /* istanbul ignore next, $flow-disable-line */
    if (typeof Promise !== 'undefined' && isNative(Promise)) {
      const p = Promise.resolve()
      timerFunc = () => {
        p.then(flushCallbacks)
        // In problematic UIWebViews, Promise.then doesn't completely break, but
        // it can get stuck in a weird state where callbacks are pushed into the
        // microtask queue but the queue isn't being flushed, until the browser
        // needs to do some other work, e.g. handle a timer. Therefore we can
        // "force" the microtask queue to be flushed by adding an empty timer.
        if (isIOS) setTimeout(noop)
      }
      isUsingMicroTask = true
    } else if (!isIE && typeof MutationObserver !== 'undefined' && (
      isNative(MutationObserver) ||
      // PhantomJS and iOS 7.x
      MutationObserver.toString() === '[object MutationObserverConstructor]'
    )) {
      // Use MutationObserver where native Promise is not available,
      // e.g. PhantomJS, iOS7, Android 4.4
      // (#6466 MutationObserver is unreliable in IE11)
      let counter = 1
      const observer = new MutationObserver(flushCallbacks)
      const textNode = document.createTextNode(String(counter))
      observer.observe(textNode, {
        characterData: true
      })
      timerFunc = () => {
        counter = (counter + 1) % 2
        textNode.data = String(counter)
      }
      isUsingMicroTask = true
    } else if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) {
      // Fallback to setImmediate.
      // Techinically it leverages the (macro) task queue,
      // but it is still a better choice than setTimeout.
      timerFunc = () => {
        setImmediate(flushCallbacks)
      }
    } else {
      // Fallback to setTimeout.
      timerFunc = () => {
        setTimeout(flushCallbacks, 0)
      }
    }
    
    export function nextTick (cb?: Function, ctx?: Object) {
      let _resolve
      callbacks.push(() => {
        if (cb) {
          try {
            cb.call(ctx)
          } catch (e) {
            handleError(e, ctx, 'nextTick')
          }
        } else if (_resolve) {
          _resolve(ctx)
        }
      })
      if (!pending) {
        pending = true
        timerFunc()
      }
      // $flow-disable-line
      if (!cb && typeof Promise !== 'undefined') {
        return new Promise(resolve => {
          _resolve = resolve
        })
      }
    }
    
    

    参考文献

    Vue官网-异步更新队列
    浅谈vue之 this.$nextTrick()

    相关文章

      网友评论

          本文标题:nextTick和异步更新视图

          本文链接:https://www.haomeiwen.com/subject/spezbktx.html