learn-(Uni-app)输入框u-search父子组件与input输入框(防抖与搜索触发)

(1)父组件
javascript 复制代码
<!-- 父组件 -->
<template>
<div>
    <searchBar @change="change" @search="search"></searchBar> 
</div>
</template>
<script>
// 子组件搜索
import searchBar from "@/components/searchBar.vue";
export default {
    data() {
        return {
            pageSize: 10, //每页条数
            pages: 1, //当前页数
            pageCount: 0, //总页数
            timer: null, //函数防抖定时器
            searchName: "", //搜索名称
            dataList: [], //数据
        };
    },
    created() {
        this.init();
    },
    components: { searchBar },
    methods: {
        // 初始化执行
        init() {
            this.getData()
        },
        // 输入框值发生变化时触发
        change(e) {
            this.searchName = e;//输入框值赋值
            // 防抖处理 防止输入框频繁触发请求
            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                this.pages = 1; //当前页数
                this.pageCount = 0; //总页数
                this.init();//初始化方法
            }, 1000);
        },
        // 确定搜索时触发,回车键,手机键盘右下角的"搜索"键时触发
        search(e) {
            this.searchName = e;
            this.pages = 1; //当前页数
            this.pageCount = 0; //总页数
            this.init();
        },
        // 获取讲师培训数据
        getData() {
            let params = {
                queryData: {
                    pageNo: this.pages,
                    pageSize: this.pageSize,
                    sortField: "",
                    sortOrder: "asc",
                    params: {
                        course_Name: this.searchName,
                    },
                },
            };
            getDataApi.getData(params).then((res) => {
                this.dataList = res.result.data;
            })
            .catch((e) => {
            });
        },
    },
};
</script>
<style lang="less" scoped></style>
(2)子组件
javascript 复制代码
<!-- 子组件-搜索 -->
<template>
    <div>
        <u-search placeholder="请输入" :showAction="false" v-model="value" @change="change" @search="search"></u-search>
    </div>
</template>
<script>
export default {
    data() {
        return {
            value: "", //搜索内容
        };
    },
    methods: {
        // 输入框值变化时触发
        change() {
            this.$emit("change", this.value);
        },
        // 确定搜索时触发,回车键,手机键盘右下角的"搜索"键时触发
        search() {
            this.$emit("search", this.value);
        },
    },
};
</script>
<style lang="less" scoped></style>
2.非父子组件input输入框

代码如下

javascript 复制代码
<template>
  <div>
    <input type="text" @input="change" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      courseName: '',
      pages: 1,
      pageCount: 0,
      timer: null
    };
  },
  methods: {
    change(e) {
      this.courseName = e.target.value;
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.pages = 1;
        this.pageCount = 0;
        this.init();
      }, 500);
    },
    init() {
      // 数据初始化逻辑
    }
  }
};
</script>

用户在输入框中输入内容时,change方法会被触发,进行防抖处理,并在延迟结束后调用init方法进行数据初始化。

3.setTimeout()和setInterval()

js-setTimeout()和setInterval()同样执行倒数100秒区别(setTimeout精度更高)_js settimeout回调和setinterval谁的效率高-CSDN博客

相关推荐
CHB16 小时前
一文讲透原生渲染和自渲染
flutter·react native·uni-app
码兄科技6 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
2501_916007476 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008896 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916008896 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
郑州光合科技余经理7 天前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
白远山7 天前
城市电竞陪玩调度系统实战:从派单算法到多端协同的架构拆解
java·架构·uni-app·需求分析
白远山8 天前
上海24小时自助健身房系统软件开发实战指南
java·架构·uni-app·需求分析
白远山9 天前
上海24小时自助健身房系统软件开发实战指南:从需求到部署
java·架构·uni-app·需求分析
海鸥两三9 天前
小程序文档预览 · 进阶面试题
小程序·uni-app·uniapp