搜索框实时搜索效果
<template> <div class="search-input"> <i class="icon-search"></i> <input class="input-inner" v-model="query" :placeholder="placeholder" /> <i v-show="query" class="icon-dismiss" @click="clear" ></i> </div> </template> <script> import { debounce } from 'throttle-debounce' export default { name: 'search-input', props: { modelValue: String, placeholder: { type: String, default: '搜索歌曲、歌手' } }, data() { return { query: this.modelValue } }, created() { this.$watch('query', debounce(300, (newQuery) => { this.$emit('update:modelValue', newQuery.trim()) })) this.$watch('modelValue', (newVal) => { this.query = newVal }) }, methods: { clear() { this.query = '' } } } </script> <style lang="scss" scoped> .search-input { display: flex; align-items: center; box-sizing: border-box; width: 100%; padding: 0 6px; height: 32px; background: $color-highlight-background; border-radius: 6px; .icon-search { font-size: 24px; color: $color-text-d; } .input-inner { flex: 1; margin: 0 5px; line-height: 18px; background: $color-highlight-background; color: $color-text; font-size: $font-size-medium; outline: 0; &::placeholder { color: $color-text-d; } } .icon-dismiss { font-size: 16px; color: $color-text-d; } } </style>
<template> <div class="search-input"> <i class="icon-search"></i> <input class="input-inner" v-model="query" :placeholder="placeholder" /> <i v-show="query" class="icon-dismiss" @click="clear" ></i> </div> </template> <script> import { debounce } from 'throttle-debounce' export default { name: 'search-input', props: { modelValue: String, placeholder: { type: String, default: '搜索歌曲、歌手' } }, data() { return { query: this.modelValue } }, created() { this.$watch('query', debounce(300, (newQuery) => { this.$emit('update:modelValue', newQuery.trim()) })) this.$watch('modelValue', (newVal) => { this.query = newVal }) }, methods: { clear() { this.query = '' } } } </script> <style lang="scss" scoped> .search-input { display: flex; align-items: center; box-sizing: border-box; width: 100%; padding: 0 6px; height: 32px; background: $color-highlight-background; border-radius: 6px; .icon-search { font-size: 24px; color: $color-text-d; } .input-inner { flex: 1; margin: 0 5px; line-height: 18px; background: $color-highlight-background; color: $color-text; font-size: $font-size-medium; outline: 0; &::placeholder { color: $color-text-d; } } .icon-dismiss { font-size: 16px; color: $color-text-d; } } </style>
主要用的是 debounce 这个库, 类似于 定时器的定时功能
debounce 实现
由于 debounce
n 只是往后推延函数的执行时间,并不具有 throttle
每隔一段时间一定会执行的能力,所以其实现起来更加简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function debounce(delay, callback) { let timeoutID; function wrapper() { const self = this ; const args = arguments; function exec() { callback.apply(self, args); } clearTimeout(timeoutID); timeoutID = setTimeout(exec, delay); } return wrapper; } |
越努力越幸运
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决