Vue 监听的几种写法

1.监听对象
watch: {
  'form.linkNum': {
    handler: function() {
      alert('我正在监听中')
    }
  }
},
watch: {
 'form.linkNum'(newValue, oldValue) {
   alert(oldValue)
   alert(newValue)
 }
}

监听字段不允许输入特殊字符

watch: {
    'form.comment': {
      deep: true,
      handler(val) {
        this.$nextTick(() => {
          const pat = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~!@#¥%&*()\-+={}|《》?:“”【】、;‘’,。、]/im
          if (pat.test(val)) {
            this.form.comment = val.slice(0, val.length - 1)
          }
        })
      }
    }
  },

监听整个表单Object

watch: {
    editForm: {
      handler(n, o) {
        // do something
      },
      deep: true// 深度监听父组件传过来对象变化
    }
  },

以后补充


版权声明:本文为weixin_43484014原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。