码迷,mamicode.com
首页 > 其他好文 > 详细

this.$nextTick()

时间:2020-02-13 14:50:24      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:class   init   tick   cas   width   结果   ini   ice   methods   

 

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue2.js"></script>
  </head>
  <body>
    <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()的作用是,等你页面上的刷新完之后,我再执行回调函数中的方法
            this.$nextTick(() => {
              console.log(document.getElementById(myh).innerText)
            })
          }
        }
      })
    </script>
  </body>
</html>

 

this.$nextTick()

标签:class   init   tick   cas   width   结果   ini   ice   methods   

原文地址:https://www.cnblogs.com/javascript9527/p/12303384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!