vue实现历史搜索

本文介绍了如何使用Vue来实现历史搜索功能。主要包括:初始化时从缓存获取数据,监听输入框值变化,判断并处理历史搜索项的增删,以及确保历史记录数组长度不超过5条。
摘要由CSDN通过智能技术生成

思路:
1. 一进入到页面,就获取缓存当中的数据;
2. 当点击input搜索框时,拿到input双向数据绑定的值;
3. 判断缓存中有没有值,没有就存当前的值;
4. 如果缓存当中有值,判断是不是重复值,是就删除当前值,并重新缓存到数组头部,不是就缓存到数组头部;
5. 判断数组长度若是大于5,就删除数组尾部的值.
在这里插入图片描述

<uni-nav-bar fixed="true" style="display: flex;justify-content: center; margin-top: 15px;" :status-bar="true" :border="false" class="NavBar"
		 :rightText="searchtext" left-icon="back" @clickLeft="goBack()">
			<view class="top_flex">
				<view class="user_top_seach">
					<u-icon size="50" name="search" color="#999" @click="searchAll" @keyup.enter="searchAll"></u-icon>
					<input class
实现Vue表单的历史记录搜索,你可以使用localStorage来存储历史搜索记录,然后在表单中展示这些记录。以下是一个简单的实现示例: 1. 在Vue组件的data中添加一个history数组,用于存储历史搜索记录。 ``` data() { return { history: [] }; } ``` 2. 在表单中添加一个输入框和一个按钮,用于进行搜索。在输入框中添加一个v-model指令,将输入框的值绑定到组件的searchText属性上。按钮的点击事件调用search方法,将搜索内容存储到history数组中。 ``` <template> <div> <input type="text" v-model="searchText" /> <button @click="search">Search</button> </div> </template> <script> export default { data() { return { history: [], searchText: '' }; }, methods: { search() { if (this.searchText) { this.history.push(this.searchText); localStorage.setItem('history', JSON.stringify(this.history)); // perform search } } } }; </script> ``` 3. 在组件的created钩子中,读取localStorage中的历史记录,并将其赋值给history数组。 ``` created() { const history = localStorage.getItem('history'); if (history) { this.history = JSON.parse(history); } } ``` 4. 在表单下方添加一个历史记录列表,展示历史搜索记录。使用v-for指令遍历history数组,将每个搜索记录展示为一个列表项。为了方便用户点击历史记录进行搜索,给每个列表项添加一个@click事件,将点击的记录赋值给searchText属性,并调用search方法进行搜索。 ``` <template> <div> <input type="text" v-model="searchText" /> <button @click="search">Search</button> <ul> <li v-for="record in history" @click="searchText = record; search">{{ record }}</li> </ul> </div> </template> ``` 这样,就可以实现Vue表单的历史记录搜索了。每次进行搜索时,都会将搜索内容存储到localStorage中,下次打开页面时,历史记录会自动加载。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值