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>
相关推荐
Mr_Mao2 小时前
Naive Ultra:中后台 Naive UI 增强组件库
前端
前端小趴菜054 小时前
React-React.memo-props比较机制
前端·javascript·react.js
摸鱼仙人~5 小时前
styled-components:现代React样式解决方案
前端·react.js·前端框架
sasaraku.5 小时前
serviceWorker缓存资源
前端
RadiumAg6 小时前
记一道有趣的面试题
前端·javascript
yangzhi_emo7 小时前
ES6笔记2
开发语言·前端·javascript
yanlele7 小时前
我用爬虫抓取了 25 年 5 月掘金热门面试文章
前端·javascript·面试
中微子8 小时前
React状态管理最佳实践
前端
米粒宝的爸爸8 小时前
uniapp在app端,在导航栏设置自定义按钮
uni-app
烛阴8 小时前
void 0 的奥秘:解锁 JavaScript 中 undefined 的正确打开方式
前端·javascript