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

相关推荐
云上凯歌1 天前
01_AI工具平台项目概述.md
人工智能·python·uni-app
郑州光合科技余经理1 天前
O2O上门预约小程序:全栈解决方案
java·大数据·开发语言·人工智能·小程序·uni-app·php
2501_916008891 天前
在不越狱前提下导出 iOS 应用文件的过程,访问应用沙盒目录,获取真实数据
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915106321 天前
Android和IOS 移动应用App图标生成与使用 Assets.car生成
android·ios·小程序·https·uni-app·iphone·webview
木子啊1 天前
UNIAPP移动端瀑布流列表,支持APP、微信小程序、H5
uni-app·瀑布流·两列排序
2501_915918411 天前
Mac 抓包软件有哪些?Charles、mitmproxy、Wireshark和Sniffmaster哪个更合适
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS 抓包绕过 SSL 证书认证, HTTPS 暴力抓包、数据流分析
android·ios·小程序·https·uni-app·iphone·ssl
WeiAreYoung1 天前
uni-app xcode 制作iOS Notification Service Extension 远程推送图文原生插件
ios·uni-app·xcode
2501_915921431 天前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS App 测试方法,Xcode、TestFlight与克魔(KeyMob)等工具组合使用
android·macos·ios·小程序·uni-app·iphone·xcode