Application of computed and watch in vue project

watch: A data change affects multiple data changes

Example: The total amount of expenses receivable is changed by the change of multiple pieces of receivable data

<div>应收: {
   
   { historyCostTotal.toLocaleString() }}元</div>

    computed: {
      //  应收
      historyCostTotal() {
        let all = 0
        this.historyContractList.forEach(item => {
          all += Number(item.receivableAmount)
          this.list[0].amountPaid = all
        })
        return all
      },
    },

 watch: A data change affects multiple data changes

Example: When the time changes, the header time of the form also changes.

getLabel为子组件的渲染表头的方法————month月份变化
getList为子组件调用接口的方法————params传值变化


watch: {
      params: {
        deep: true,
        handler(newV, oldV) {
          this.params = newV;
          if (newV && this.type == '2' && this.channelFlag == '0') {
            this.getList();
          }
        }
      },
    month: {
      deep: true,
      handler(newV, oldV) {
        this.month = newV;
        if (newV) {
          this.getLabel(this.timeType)
        }
      }
    },
  },

Guess you like

Origin blog.csdn.net/m0_65274248/article/details/128058094