this.$ nextTick()方法主要是用在数据改变,dom改变应用场景中。vue中数据和dom渲染由于是异步的,所以,要让dom结构随数据改变这样的操作都应该放进this.$nextTick()的回调函数中
<template>
<div class="list">
<div ref="hello">
{{helloVal}}
<el-button type="success" @click="toClick">点我</el-button>
</div>
</div>
</template>
<script>
export default {
data(){
return {
helloVal:'你好'
}
},
methods:{
toClick(){
this.helloVal = "萨瓦迪卡";
console.log(this.$refs['hello'].innerText);
this.$nextTick(()=>{
console.log(this.$refs['hello'].innerText)
})
}
}
}
</script>