Vue2中, 监听对象的某个属性

监测对象{}中的某个属性.

  • 方法一: 只使用 watch方法
 new Vue({
    
    
      el: '#app',
      data(){
    
    
        return {
    
    
          // 初始的数据对象
          rowObj:{
    
    
            rowUrl:'',
            // 其他数据
            name: 'kobe',
            age:43
          }
        }
      },
      watch:{
    
    
      // 直接检测想要检测的对象的属性: 
        'rowObj.rowUrl'(newVal, oldVal){
    
    
			console.log(oldVal, newVal)
        }
      }
    })

  • 方法二: 使用watch+ computed结合:
 new Vue({
    
    
      el: '#app',
      data(){
    
    
        return {
    
    
          // 初始的数据对象
          rowObj:{
    
    
            rowUrl:'',
            // 其他数据
            name: 'kobe',
            age:43
          }
        }
      },
    computed: {
    
    
		rowUrl(){
    
    
			return this.rowObj.rowUrl
		}	
	},
      watch:{
    
    
        rowUrl(newVal, oldVal){
    
    
			console.log(oldVal, newVal)
        }
      }
    })

猜你喜欢

转载自blog.csdn.net/weixin_40944062/article/details/113852762