vue監聽input是否為空(監聽值為物件某個屬性)

只有吃吃吃發表於2020-04-09
 <script>
 computed: {
    search() {
      return this.myUaData.searchSite;
    }
  },
  watch: {
    search(newValue, oldValue) {
      if (newValue === "") {
        //如果為空,執行方法獲取list
        this.getList();
      }
    }
  }
</script>
<template>
          <el-input
            placeholder="請輸入關鍵字搜尋"
            icon="search"
            v-model.trim="myUaData.searchSite"
            size="medium"
            class="search-input"
            @keyup.native.enter="getList()"
            clearable
          ></el-input>
</template>

 

補充:若監聽的陣列物件 ,則用

    search: {
      handler: function(newVal, oldVal) {},
      deep: true
    }

另外存在一種情況:watch初次觸發的時需要初始化,可能導致監聽不生效,通過immediate來控制 ,true則立即執行,false則初始化不執行

    search: {
      immediate: true,
      handler: function(newVal, oldVal) {
      }
    },

 

相關文章