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博客

相关推荐
小徐_23339 小时前
Wot UI v1 升级 v2?这份迁移指南帮你少踩坑!
前端·微信小程序·uni-app
游戏开发爱好者814 小时前
使用Fiddler设置HTTPS抓包诊断Power Query网络问题
android·ios·小程序·https·uni-app·iphone·webview
棋宣17 小时前
uni-app编译到微信小程序中,父传子props首次传递数据不接收的bug
微信小程序·uni-app·bug
阳光先做2 天前
uniapp打包鸿蒙安装包问题
uni-app
码海扬帆:前端探索之旅2 天前
深度定制 uni-combox:新增功能详解与实战指南
前端·vue.js·uni-app
计算机学姐2 天前
基于微信小程序的图书馆座位预约系统【uniapp+springboot+vue】
vue.js·spring boot·微信小程序·小程序·java-ee·uni-app·intellij-idea
中犇科技2 天前
电商app源码系统推荐|开源 uniapp 商城系统
uni-app
海水冷却3 天前
uniapp 实现直播功能的完整方案与实战指南
uni-app
wuxianda10303 天前
Object-C/Swift/UniApp项目苹果商店上架3天极速解决方案汇报总结
ios·uni-app·objective-c·cocoa·苹果上架
WKK_3 天前
uniapp 微信小程序使用TextEncoder,arrayBufferToBase64
微信小程序·小程序·uni-app