computed计算属性依赖的响应式属性为对象obj时,对象obj中有属性字段【A、B、C】时,对A、B进行了if判断,那么只有A、B变化时,计算属性才更新,否则不更新

computed计算属性依赖的响应式属性为对象obj时,对象obj中有属性字段【A、B、C】时,对A、B进行了if判断,那么只有A、B变化时,计算属性才更新,否则不更新

注意并不是obj变化时,omputed计算属性就更新;而是obj中对应computed计算属性依赖的相应属性【A、B】变化时才会更新
点击onSubmitA,才会触发监听formData;
点击onSubmitB,不会触发监听formData;
<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="a">
      <el-input v-model="form.a"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmitA">a++</el-button>
      <el-button type="primary" @click="onSubmitB">b++</el-button>
    </el-form-item>
  </el-form>

</template>
<script>
export default {
  name: 'couponForm',
  computed: {
    formData () {
      return {
        a: this.form.a,
        b: 1
      }
    }
  },
  watch: {
    formData: {
      deep: true,
      handler (newVal, oldVal) {
        console.log('newVal', newVal)
        console.log('oldVal', oldVal)
        console.log('newVal===oldVal?', newVal === oldVal)
        console.log('newVal==oldVal?', JSON.stringify(newVal) === JSON.stringify(oldVal))
      }
    }
  },
  data () {
    return {
      form: {
        a: 1,
        b: 2
      }

    }
  },
  methods: {
    onSubmitA () {
      this.form.a = this.form.a + 1
    },
    onSubmitB () {
      this.form.b = this.form.b + 1
    }
  }
}
</script>

 

posted @ 2019-07-04 14:13  sakura-sakura  阅读(1092)  评论(0编辑  收藏  举报