HarmonyOS 封装搜索历史工具(适用API12以上版本)

HarmonyOS 

封装搜索历史工具

目标:基于首选项封装搜索历史存储工具

import { preferences } from '@kit.ArkData'

class History {
  store?: preferences.Preferences

  getStore(){
    if(!this.store){
     const ctx = AppStorage.get<Context>('context')
     this.store = preferences.getPreferencesSync(ctx, {name: 'history-store'})
    }
    return this.store
  }

  /**
   * 存储搜索
   */
  async setItem(keyword: string){
    this.getStore().putSync(keyword, keyword)
    await this.getStore().flush()
  }

  /**
   * 删除一个
   */
  async delItem(keyword: string){
    this.getStore().deleteSync(keyword)
    await this.getStore().flush()
  }

  /**
   * 删除所有
   */
  async clear(){
    this.getStore().clearSync()
    await this.getStore().flush()
  }

  /**
   * 取出搜索
   */
  getAll(){
    // {ts: 'ts', js: 'js', vue: 'vue'}
    // Object.keys() 取出所有的键  Object.values() 取出所有的值
    const obj = this.getStore().getAllSync()
    return Object.keys(obj)
  }
}

export  const history = new History()

34.子传父(用函数传)

//子组件
  onSearch: (val: string) => void = () => { }


.onClick(() => {
                this.onSearch(keyword)
            })




//父组件接收
SearchHistory({
            onSearch: keyword => {
              this.keyword = keyword
       }
   })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值