this.$nextTick():dom更新后执行
举例如下:
//--------------html--------------
<div ref="textDiv">{{txt}}</div>
<button @click="changeTxt">点击修改</button>//--------------data--------------
data() {return {txt:'未修改',}
}//--------------methods--------------
methods: {changeTxt(){this.txt = '修改后';console.log('未使用this.$nextTick():',this.$refs['textDiv'].innerText);this.$nextTick(()=>{console.log('使用this.$nextTick():', this.$refs['textDiv'].innerText);})}
},
页面:
点击按钮:
输出: