vue 使用watch同时监听多个属性


watch监听调用了两个参数,两个参数的出现都调用了一个函数,分开检测就会调用两次

第一种方法

data () {
  return {
     city: '',
     area: '',
     currentPage: ''
  }
},
 
watch: {
   city: function (val) {
 
      // 写要执行的代码片段
 
   },
   area: function (newVal,oldVal) {
 
      // 写要执行的代码片段
 
   }, 
}

第二种


data () {
  return {
     city: '',
     area: '',
     currentPage: ''
  }
},
 
computed: {
    listenChange () {
        const { city, area, currentPage } = this
        return { city, area, currentPage }
    },
},
 
watch: {
   listenChange (val) {
 
      // 写要执行的代码片段
 
   },
    
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM