在vue中,我们可能需要再created中获取到dom元素进行操作,但是呢在created中获取dom元素会获取不到,应为dom还没有渲染完,找不到dom元素导致获取不到,所以可以在 this.$nextTick()延迟加载中去获取dom元素
<div ref="audioPlayer">
我是元素
</div>
created() {
//在这里获取不到div元素,应为dom还没有渲染加载完,找不到div
//const audioPlayer = this.$refs.audioPlayer;
//延迟执行
this.$nextTick(() => {
//在这里才能获取到,这时dom渲染加载完了
const audioPlayer = this.$refs.audioPlayer;
});
},
created() {
this.$nextTick(() => {
console.log(4663)
this.playAudio()
});
},