vue2.6.11版本的nextTick

Posted 万年打野易大师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue2.6.11版本的nextTick相关的知识,希望对你有一定的参考价值。

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.
  // Technically 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)
  }
}

Promise > MutationObserver > setImmediate > setTimeout

MutationObserver能监听一个DOM对象上发生的子节点删除、属性修改、文本内容修改等

let mo = new MutationObserver(callback);
let domTarget = 你想要监听的dom节点;
mo.observe(domTarget, {
      characterData: true //说明监听文本内容的修改。
})

熟悉任务执行机制就能了解

以上是关于vue2.6.11版本的nextTick的主要内容,如果未能解决你的问题,请参考以下文章

Vue2最低支持Node版本调查

事件在部分区域生效

Vuex 工作原理

Vue报错: this.getOptions is not a function

实现多列下拉框组件的另一种思路(element-ui 多列下拉框)

如何管理在每个 git 版本中添加私有代码片段?