当前位置: 首页 > article >正文

Vue.nextTick 的工作机制

Vue.nextTick 是异步执行的,它使用了微任务(microtasks)或宏任务(macrotasks),具体使用哪种取决于 JavaScript 环境的支持情况。具体来说,Vue 优先选择使用微任务,如 Promise.then,如果不支持微任务,则退而使用宏任务,如 setTimeout

缓存内容

Vue.nextTick 会将回调函数缓存到一个队列中,这个队列中的所有回调函数会在 DOM 更新完成之后被依次执行。具体来说,Vue 会维护一个内部的回调队列,当你调用 Vue.nextTick 时,它会将回调函数添加到这个队列中,并确保在下一次 DOM 更新循环之后执行这些回调。

示例

javascript复制代码new Vue({
  data: {
    message: 'Hello Vue!'
  },
  methods: {
    updateMessage() {
      this.message = 'Hello World!';
      this.$nextTick(() => {
        // This will be executed after the DOM is updated
        console.log('DOM updated');
      });
    }
  }
});

在上述示例中,this.$nextTick 确保了 console.log('DOM updated') 只会在 DOM 更新完成后执行。

实现机制

Vue.nextTick 使用了一种合适的策略来选择微任务或宏任务,如下所示:

  1. 微任务(microtasks)优先:如果环境支持 Promise,则优先使用 Promise.then 来实现微任务。这是因为微任务会在当前事件循环的末尾执行,非常高效。

  2. 宏任务(macrotasks)作为备用:如果环境不支持微任务,则使用宏任务(如 setTimeout)作为备用方案。

Vue.nextTick 的源代码

以下是 Vue.js 2.x 中 nextTick 的简化实现:

javascript复制代码import { noop } from 'shared/util';
import { handleError } from './error';
import { isIOS, isNative } from './env';
​
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.
let timerFunc;
​
if (typeof Promise !== 'undefined' && isNative(Promise)) {
  const p = Promise.resolve();
  timerFunc = () => {
    p.then(flushCallbacks);
    if (isIOS) setTimeout(noop);
  };
} else if (!isIE && typeof MutationObserver !== 'undefined' && (
  isNative(MutationObserver) ||
  MutationObserver.toString() === '[object MutationObserverConstructor]'
)) {
  // use MutationObserver where native Promise is not available,
  // e.g. PhantomJS, iOS7, Android 4.4
  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);
  };
} else if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) {
  timerFunc = () => {
    setImmediate(flushCallbacks);
  };
} else {
  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.nextTick 缓存的内容是一个回调函数队列。这些回调函数会在下一次 DOM 更新循环之后执行。Vue 使用微任务(如 Promise.then)或宏任务(如 setTimeout)来确保这些回调函数的执行时机。这个机制确保了在进行 DOM 操作时,所有数据的变化都已经反映到 DOM 上,从而避免了可


http://www.kler.cn/news/308840.html

相关文章:

  • 【乐企-业务篇】开票前置校验服务-规则链服务接口实现(纳税人基本信息)
  • 基于SpringBoot+Vue+MySQL的网上甜品蛋糕售卖店管理系统
  • android 老项目中用到的jar包不存在,通过离线的方法加载
  • 项目实战应用Redis分布式锁
  • wordpress不同网站 调用同一数据表
  • Mac虚拟机Parallels Desktop 20 for Mac破解版发布 完整支持 Windows 11
  • leetcode-560. 和为 K 的子数组
  • Qt构建JSON及解析JSON
  • JMeter 中使用 Gson 操作请求中的Boby参数
  • SQL超时的常见原因和解决思路
  • MFC实现对话框与控件的自适应调节
  • 20个Python入门基础语法要点
  • NISP 一级 | 6.2 移动智能终端安全威胁
  • 使用Mockito进行单元测试
  • 春意教育:SpringBoot在线学习平台开发
  • 矢量化操作
  • JS日期转化指定格式,获取月/周日期区间
  • CentOS 中配置 OpenJDK以及多版本管理
  • Unix-like系统是什么
  • 408算法题leetcode--第五天
  • frp内网穿透功能使用教程
  • 玩机搞机-----如何简单的使用ADB指令来卸载和冻结系统应用 无需root权限 详细操作图示教程
  • Spring Boot-热部署问题
  • SpringBoot启动成功,但端口启动失败
  • 架构师备考的一些思考(四)
  • 集群聊天服务器项目【C++】(六)MySql数据库
  • 【观察】戴尔AI算力加速服务器“焕新上市”,打通AI落地行业“最后一公里”...
  • 2024年上半年软考【中级】网络工程师 综合知识真题回顾
  • Bio-Linux-shell详解-2-基本Shell命令快速掌握
  • 夕阳红老年大学视频教学网站管理系统设计与实现,按用户最近浏览分类字段推荐视频课程-留言和评论分词过滤