监听单个值的变化

作用:监视单个值的改变

export default {
	watch: {
    // 基本类型值的监视
  	text: function(newVal, oldVal) {
      console.log(我改变了);
    }
    // 引用类型(对象)的监视
    person: {
    	handler: function(newVal, oldVal) {},
  		deep: true
  	}
    // 路由的变化
    '$route.path': function(newVal, oldVal) {
      if (newVal === '/login') {...} else {...}
    }
  }
}

监听多个值的变化

data() {
  return {
    city: '',
    country: ''
  }
},
computed: {
  address() {
    const { city, country } = this
    return {
      city,
      country
    }
  }
},
watch: {
  address: {
    handler: function(val) {
      console.log('address change: ', val)
    },
    deep: true
  }
}