@hzy1123581324/z-view-ui
Version:
z-view-ui是使用vue3开发的组件,开发中,有部分组件功能未实现,慎用
41 lines (38 loc) • 1.05 kB
JavaScript
import { reactive } from "vue";
export const useHistory = function({maxLength= 10,}={}){
const history = reactive(uni.getStorageSync("page-search-history")||[]);
/** 设置历史记录,前压入,重复删除
* @param {string} keyword
*/
function setHistory(keyword){
const findIndex = history.indexOf(keyword);
if(findIndex>-1){
history.splice(findIndex,1);
}
history.unshift(keyword);
if(history.length>maxLength){
history.length = maxLength;
}
uni.setStorageSync('page-search-history',history);
}
/** 删除某一项记录
* @param {number} index 下标
*/
function spliceHistory(index){
history.splice(index,1);
uni.setStorageSync('page-search-history',history);
}
/**
* 清空历史记录
*/
function clearHistory(){
history.length= 0;
uni.removeStorageSync("page-search-history");
}
return {
history,
setHistory,
spliceHistory,
clearHistory,
}
}