zoukankan      html  css  js  c++  java
  • vue中nextTick理解与使用

    <template>
      <div>
        <button @click="handle">添加属性</button>
        <div ref="div">{{change}}</div>
      </div>
    </template>
    
    <script>
    export default {
      name: "Mine",
      data() {
        return {
          change: "old"
        };
      },
      methods: {
        handle(){
            this.change='new'
            console.log(this.$refs.div.textContent)//old
            // 使用nextTick获取最新的DOM结构,nextTick内部返回的是一个promise
            this.$nextTick(()=>{
                console.log(this.$refs.div.textContent)//new
            })
        },
        // 所以也可以写成下面写法
        async handle() {
          this.change = "new";
          console.log(this.$refs.div.textContent);//old
          //nextTick获取最新的DOM结构,nextTick返回的是一个promise
          await this.$nextTick();
          console.log(this.$refs.div.textContent);//new
        }
      }
    };
    </script>
    
    <style lang="scss">
    </style>

    代码截图:

  • 相关阅读:
    Day60
    Day53
    Day50
    Day49
    Day48
    Day47
    Day46(2)
    Day46(1)
    Day45
    Day44
  • 原文地址:https://www.cnblogs.com/Alex-Song/p/12752796.html
Copyright © 2011-2022 走看看