当前位置:首页 > 经验笔记 > VUE > 正文内容

简单理解Vue中的nextTick

han32686年前 (2020-07-22)VUE3123
官方文档:Vue 在更新 DOM 时是异步执行的,为了在数据变化之后等待 Vue 完成更新 DOM,可以在数据变化之后立即使用 Vue.nextTick(callback)。这样回调函数将在 DOM 更新完成后被调用。
<div class="app">
  <div ref="msgDiv">{{msg}}</div>
  <div v-if="msg1">Message got outside $nextTick: {{msg1}}</div>
  <div v-if="msg2">Message got inside $nextTick: {{msg2}}</div>
  <div v-if="msg3">Message got outside $nextTick: {{msg3}}</div>
  <button @click="changeMsg">
    Change the Message  </button>
</div>
new Vue({
  el: '.app',
  data: {
    msg: 'Hello Vue.',
    msg1: '',
    msg2: '',
    msg3: ''
  },
  methods: {
    changeMsg() {
      this.msg = "Hello world."
      this.msg1 = this.$refs.msgDiv.innerHTML      
      this.$nextTick(() => {
        this.msg2 = this.$refs.msgDiv.innerHTML      })
      this.msg3 = this.$refs.msgDiv.innerHTML    }
  }})


image.png

扫描二维码推送至手机访问。

版权声明:本文由瀚文博客发布,如需转载请注明出处。

本文链接:https://hanwenblog.com/post/15.html

分享给朋友:

相关文章

vue中实现点击按钮滚动到页面对应位置 使用css3平滑属性实现

vue中实现点击按钮滚动到页面对应位置 使用css3平滑属性实现

vue项目中,需要实现点击对应按钮,滚动到对应页面位置,下面分享一个简单实用的方法<template>   <div class="box&...

vue中$set的使用踩坑日记

vue中$set的使用踩坑日记

1、为什么要用set?在vue中,并不是任何时候数据都是双向绑定的。在官方文档中,有这样一段话,如下:从文档得知,当数据没有被双向绑定的时候,我们就需要使用set了2、set用法解决数据没有被双向绑定...

VUE computed和watch的区别最简化

VUE computed和watch的区别最简化

<!DOCTYPE html> <html lang="en"> <head>    &...

基于Vue的移动端图片裁剪组件(Clipic)可自动压缩

基于Vue的移动端图片裁剪组件(Clipic)可自动压缩

安装:Bashnpm install --save clipic使用代码:Markup<template>     ...

vue强制更新$forceUpdate()

vue强制更新$forceUpdate()

vue强制更新$forceUpdate()添加this.$forceUpdate();进行强制渲染,效果实现。搜索资料得出结果:因为数据层次太多,render函数没有自动更新,需手动强制刷新。调用强制...