Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

为什么在mounted函数中有时候无法获取到root元素?

<template>
    <div class="container"></div>
</template>

export default {
  mounted () {
    const container = document.querySelector('.container')  
  }
}

??类似这样,我在mounted函数中获取这个组件的根元素,一般组件都能在mounted函数中获取到对应的根元素,因为我觉得这个时候组件已经挂载了,但是我有一个组件不知道为什么就是获取不到,最后我用一个定时器解决了,但是还是比较好奇为什么在mounted中获取不到这个问题。

??我尝试过使用nextTick,还是一样获取不到root元素。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

是不是用了v-if,一般这种情况用this.$nextTick()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...