this.$nextTick()方法的使用


作用是:等你頁面刷新完之后,再執行回調函數中的方法

<div id="app">
      <h1 id="myh">{{msg}}</h1>
      <button @click="change">點擊</button>
    </div>
    <script>
      var vm = new Vue({
        el: '#app',
        data: {
          msg: 'hello'
        },
        methods: {
          change() {

            this.msg = 'itcast'
            // console.log(document.getElementById('myh').innerText); // 如果直接這樣打印,打印出來的結果不是我們想要的itcast,而是hello,因為this.msg = ‘itcast’ 它是異步的
            this.$nextTick(() => {
              console.log(document.getElementById('myh').innerText)
            })
          }
        }
      })
    </script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM