搜索历史的js

//保存关键字到历史记录
			saveKeyword(keyword) {
				console.log(uni.getStorageSync('historyList'),'先获取缓存historyList');
				uni.getStorage({
					key: 'historyList',
					success: (res) => {
						console.log('获取缓存成功',res);
						let OldKeys = res.data || [];
						let findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'historyList',
							data: OldKeys
						});
						this.historyList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						let OldKeys = [keyword];
						uni.setStorage({
							key: 'historyList',
							data: OldKeys
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
						console.log('获取缓存失败');
					}
				});
			},

你可能感兴趣的:(uniapp,javascript,开发语言,ecmascript)