element el-dialog标签里的内容不能即时更新

<template>
<div>
  <el-dialog
    title='编辑发起人'
    class="add-reply-dialog"
    :visible.sync="true"
  >
    <span>{{form.id}}222222</span>
    <button @click="toChange">click change</button>
  </el-dialog>    
</div>
</template>
<script>
    export default {
        data() {
            return {
                form: {
                    id: '',
                }
            }
        }
        methods: {
            toChange() {
                this.form.id = 'this show show'
            }
        }
    }
</script>

上面的代码当我click的时候,span标签没有显示'this show show',但是当我把toChange方法改成这样

toChange() {
     this.form = {
         ...this.form,
         id: 'this show show',
     }
 }

就会显示正常结果,这是为什么呢?

阅读 17.5k
2 个回答

因为vue只能监听对象上已有的属性变化,form之前没有id这个属性,所以id变化了,并不会触发视图的更新

第二种直接覆盖的的form,会自动检测form的值并向下检测,检测到了id,所以会自动更新到视图上

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题