010-$nextTick

1、问题

Vue 实现响应式,在 data 更新后,一定时间内,没有继续操作DOM,然后会触发浏览器渲染引擎去更新DOM,更新DOM也是需要时间的,所以 data 更新引起的 DOM更新并不是实时的。

2、$nextTick

this.$nextTick(() => {
  // ...
});
  • $nextTick 是在下次 DOM 更新循环结束之后执⾏延迟回调,在修改数据之后使⽤ $nextTick,则可以在回调中获取更新后的 DOM,在下次 DOM 更新循环结束之后执行延迟回调。
  • 当数据更新了,在dom中渲染后,⾃动执⾏该函数。

3、应用场景

💡 Tips:显示组件,并调用组件的初始化方法

<template>
  <div id="app">
    <Son v-if="showSonComponent" ref="sonComponent" />
  </div>
</template>

<script>
import Son from './components/SlotComponents/Son'
export default {
  name: 'App',
  components: { Son },
  data() {
    return {
      showSonComponent: false
    }
  },
  methods: {
    init() {
      // 更新该变量,会引起 DOM 更新
      this.showSonComponent = true
      this.$nextTick(() => {
        // 需要等DOM更新完成后调用init方法
        this.$refs.sonComponent.init()
      })
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: #4a90e2;
  color: #fff;
  padding: 20px;
}
.slot-contant {
  color: #f00;
  font-size: 16px;
}
</style> 

相关推荐

  1. 010-$nextTick

    2024-03-12 00:58:01       43 阅读
  2. nextTick详解

    2024-03-12 00:58:01       63 阅读
  3. nextTick原理

    2024-03-12 00:58:01       41 阅读
  4. 【Vue】什么是nextTick

    2024-03-12 00:58:01       52 阅读
  5. 【vue】nextTick的使用

    2024-03-12 00:58:01       56 阅读
  6. vue中nextTick()

    2024-03-12 00:58:01       52 阅读
  7. nextTick的作用

    2024-03-12 00:58:01       40 阅读
  8. 10分钟了解nextTick,并实现简易版本的nextTick

    2024-03-12 00:58:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-12 00:58:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-12 00:58:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-12 00:58:01       82 阅读
  4. Python语言-面向对象

    2024-03-12 00:58:01       91 阅读

热门阅读

  1. 浏览器内核小知识

    2024-03-12 00:58:01       41 阅读
  2. Linux报错排查-安装PHP的remi库报错

    2024-03-12 00:58:01       44 阅读
  3. 设计模式-适配器模式

    2024-03-12 00:58:01       49 阅读
  4. 热销商品-爬虫销量信息

    2024-03-12 00:58:01       47 阅读
  5. 【PICO 4教程】Unity3D中实现对PICO 4的手柄按键响应

    2024-03-12 00:58:01       44 阅读
  6. Linux: 调用接口

    2024-03-12 00:58:01       46 阅读
  7. 使用Docker安装Redis并运行

    2024-03-12 00:58:01       47 阅读
  8. springboot之异步任务、邮件任务、定时任务

    2024-03-12 00:58:01       35 阅读
  9. 记一次面试经历

    2024-03-12 00:58:01       51 阅读