首页 > 技术文章 > vue 监听对象里某个值的变化

asker009 2020-06-08 10:09 原文

假设vue里有个form表单对象:

      form: {
        id: null,
        projectName: null,
        state: '100',
        typeId: null,
        typeName: null      
      }

typeId变化时,实时修改typeName的值(因为是回调修改,其实有点延时)

使用watch监听typeId

watch: {
    'form.typeId': {
      handler: function() {
        if (!this.form.typeId) {
          this.form.typeName = null
          return
        }
        this.form.typeName = this.projectTypeOptions.find(item => { return item.key === this.form.typeId }).display_name
      }
    }
  }

 

推荐阅读