首页 > 数据库技术 > 详细

Cannot access element shown by v-if in component mounted callback

时间:2021-01-07 18:58:00      阅读:26      评论:0      收藏:0      [点我收藏+]

Cannot access element shown by v-if in component mounted callback

Vue renders to the DOM asynchronously. So, even though you are setting your loaded property to true, the ref will not exist until the next tick in Vue‘s cycle.

To handle that, use the $nextTick method.

 

vuejs mounted is called even if component was not loaded via v-if

console.clear()

new Vue({
  el: "#app",
  data:{
    loading: true
  },
  mounted(){
    setTimeout(()=> {
      this.loading = false
      this.$nextTick(() => console.log(this.$refs.done))
    }, 1000)
  }
})

 

<script src="https://unpkg.com/vue@2.4.2"></script>
<div id="app">
  <div v-if="loading">Loading</div>
  <div ref="done" v-else>Done</div>
</div>

 

Cannot access element shown by v-if in component mounted callback

原文:https://www.cnblogs.com/chucklu/p/14248027.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!