vue需要同时监听两个变量怎么做

第一种方法:先将两个变量合并,然后监听合并之后的变量

watch: {
    mergeVariable(val) { //新增子节点或者修改节点
      if(val.dialogStatus == 'child'){ //新增子节点
        this.restForm()
        this.form.pid = val.detailData.id
        this.viewModal = false
      }else if(val.dialogStatus == 'edit'){//修改节点
        this.form = val.detailData
        this.viewModal = false
      }else if(val.dialogStatus == 'view'){//查看
        this.form = val.detailData
        this.viewModal = true
      }else if(val.dialogStatus == 'add') { //新增根节点
        this.viewModal = false
      }
    }
  },
  computed: {
    mergeVariable() {
      const { dialogStatus, detailData } = this
      return {
        dialogStatus,
        detailData,
      }
    },
  },

第二种方法:分别监听两种变量,但是将变化之后的操作提取成一个函数,放在methods中

watch: {
    dialogStatus(){
      this.operatePanel()
    },
    'detailData.yztTreeEntity.id'(){
      this.operatePanel()
    },
    
  },
methods: { 
    operatePanel() {
        //操作
    }
},

你可能感兴趣的:(学习vue,vue.js,前端,javascript)