computed計算屬性依賴的響應式屬性爲對象obj時,對象obj中有屬性字段【A、B、C】時,對A、B進行了if判斷,那麼只有A、B變化時,計算屬性才更新,不然不更新

computed計算屬性依賴的響應式屬性爲對象obj時,對象obj中有屬性字段【A、B、C】時,對A、B進行了if判斷,那麼只有A、B變化時,計算屬性才更新,不然不更新web

注意並非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>
相關文章
相關標籤/搜索