<template>
<div>
{{ msg }}
</div>
</template>
<script>
export default {
data() {
return {
queryObj: {
attr1: '',
attr2: '',
attr3: '',
attr4: ''
},
msg: ''
}
},
watch: {
queryObj: {
handler() {
this.msg += '<br>改变了'
},
deep: true
}
},
mounted() {
// 第一种情况
this.queryObj.attr3 = '2021-03-13'
this.queryObj.attr4 = '2021-03-14'
// 第二种情况
// this.queryObj.attr3 = '2021-03-13'
// this.$nextTick(() => {
// this.queryObj.attr4 = '2021-03-14'
// })
}
}
</script>
第一种情况 页面只打印了1次
第二种情况 页面只打印了2次预格式化文本
vue的优化
第一种情况,2次赋值合并成了1次更新,只触发1次
第二种情况,在第1次赋值更新渲染完成后,又执行了1 次赋值,又执行了更新,所以是2次