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>
相关推荐
十八朵郁金香几秒前
通俗易懂的DOM1级标准介绍
开发语言·前端·javascript
GDAL1 小时前
HTML 中的 Canvas 样式设置全解
javascript
m0_528723811 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer1 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
GDAL1 小时前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
禾苗种树1 小时前
在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
前端·vue.js·echarts
GISer_Jing1 小时前
Javascript排序算法(冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序)详解
javascript·算法·排序算法
贵州数擎科技有限公司2 小时前
使用 Three.js 实现流光特效
前端·webgl
JustHappy2 小时前
「我们一起做组件库🌻」做个面包屑🥖,Vue的依赖注入实战💉(VersakitUI开发实录)
前端·javascript·github
祝鹏2 小时前
前端如何制定监控项
前端