uni-app:监听数据变化(watch监听、@input事件)

方法一:文本框监听,使用@input事件

html 复制代码
<template>
  <view>
    <input type="text" v-model="wip_entity_name" @input="handleInputChange" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      wip_entity_name: '' // 文本框绑定的数据
    };
  },
  methods: {
    handleInputChange() {
      // 执行需要在文本框值改变时执行的方法
      console.log('文本框的值发生改变');

      // 调用其他方法
      this.otherMethod();
    },
    otherMethod() {
      // 实现其他方法的逻辑
      console.log('执行其他方法');
    }
  }
};
</script>

方法二:使用watch监听属性(很好解决了文本框中数据非手输时监听不到数据变化)

html 复制代码
<template>
  <view>
    <input type="text" v-model="wip_entity_name" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      wip_entity_name: '' // 文本框绑定的数据
    };
  },
  watch: {
    wip_entity_name(newVal, oldVal) {
      // 监听文本框值的改变
      if (newVal !== oldVal) {
        // 执行需要在文本框值改变时执行的方法
        console.log('文本框的值发生改变');
        // 调用其他方法
        this.otherMethod();
      }
    }
  },
  methods: {
    otherMethod() {
      // 实现其他方法的逻辑
      console.log('执行其他方法');
    }
  }
};
</script>
相关推荐
沙漠之主2 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
hasty2 小时前
不上传新包,也能改变用户拿到的版本:npm dist-tag 的 OIDC 权限治理
前端·npm·node.js
Csvn3 小时前
diff 算法(虚拟 DOM Reconciliation)
前端
前端snow4 小时前
ai agent --- 文件存储
前端
Frag0ut4 小时前
Chrome四大版本获取及共存指南:Stable/Beta/Dev/Canary
前端·chrome·浏览器·dev·beta·canary·共存版
IT_陈寒5 小时前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
广州华水科技5 小时前
单北斗GNSS变形监测在大坝安全监测中的应用与优势
前端
zeng不错6 小时前
B站 173 个投稿活动看到眼瞎?我写了个扩展,主打一个精准打击
前端
Latchh6 小时前
前端导出的JPEG红字发糊,质量要拉到100才清楚
前端·图像处理·人工智能·计算机视觉
默_笙6 小时前
🚦 请三个 AI 考官给 RAG 打分:量化评估实战(下)
前端·javascript