• setup中的nextTick函数


    await nextTick()Vue 3 的一个异步函数,用于等待 DOM 更新完成后执行回调函数,
    它在 setup 函数中非常有用,可以确保在对 DOM 进行操作之前,先等待 Vue 完成相关的 DOM 更新。

    下面是一个示例,演示了 await nextTick() 的用法:

    <template>
      <div>
        <p ref="message">{{ message }}</p>
        <button @click="updateMessage">更新消息</button>
      </div>
    </template>
    
    <script>
    import { ref, nextTick } from 'vue';
    
    export default {
      setup() {
        const message = ref('Hello, Vue 3!');
    
        // 更新消息
        async function updateMessage() {
          message.value = '更新中...';
          await nextTick(); // 等待 DOM 更新
          message.value = '消息已更新!';
        }
    
        return {
          message,
          updateMessage
        };
      }
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    这里使用 await nextTick() 来确保在更新消息之前等待 Vue 完成相关的 DOM 更新。

    首先,当点击按钮时,updateMessage 函数会先将 message 的值更新为 “更新中…”,
    然后,使用 await nextTick() 等待 DOM 更新完成,
    再然后,一旦 DOM 更新完成,message 的值会再次更新为 “消息已更新!”,并且在页面上显示出来。

    使用 await nextTick() 可以确保在对 DOM 进行操作之前,先等待 Vue 完成相关的 DOM 更新,以避免出现不一致的情况。

  • 相关阅读:
    二分+dijkstra
    cookie、localStorage 和SessionStorage的区别和特点?
    煤矿皮带跑偏监测识别系统
    python返回多个值与赋值多个值
    postgresql
    Linux 压缩和解压
    ceph高可用
    AST2500常用操作命令
    vue3+fabric绘制检测区域
    Flink(Pometheus监控)
  • 原文地址:https://blog.csdn.net/pig_ning/article/details/132717407