computed和watch在vue项目中的应用

watch:一条数据变化影响多条数据变化

举例:费用应收总计受多条应收数据变化而变化

<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:一条数据变化影响多条数据变化

举例:当时间变化,表格的表头时间也跟着变化。

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)
        }
      }
    },
  },

猜你喜欢

转载自blog.csdn.net/m0_65274248/article/details/128058094
今日推荐