当前位置: 首页 > news >正文

河南网站建设哪家公司好贵州网站公司

河南网站建设哪家公司好,贵州网站公司,自学软件网站开发,环境网站模板前言: 我们在使用很多app时,都会发现搜索记录在杀掉后台后进入依然还在,这极可能是用了持久化数据方案。这篇博客分享使用鸿蒙Preferences实现持久化方案,相信你看完后会有所收获。 我们还是老样子,跟着官方文档学习与…

前言:

我们在使用很多app时,都会发现搜索记录在杀掉后台后进入依然还在,这极可能是用了持久化数据方案。这篇博客分享使用鸿蒙Preferences实现持久化方案,相信你看完后会有所收获。

我们还是老样子,跟着官方文档学习与实践,链接如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-data-preferences#preferencesgetpreferenceshttps://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-data-preferences#preferencesgetpreferences看完了,就来做个Demo练一练,本人整理的运行效果图和代码如下:

1、Index.ets

import { getPreferencesUtil, PreferencesUtil } from "./PreferencesUtil"@Entry
@Component
export struct Index {preferences: PreferencesUtil | undefined = undefined@State message: string[] = ["Hello World", "番茄", "持久化", "杯子", "奶茶"]@State index: number = 0context: Context = this.getUIContext().getHostContext() as ContextaboutToAppear(): void {this.preferences = getPreferencesUtil()}@BuildertextBuilder() {Column({ space: 20 }) {Text(this.message[this.index])Button("持久化当前字符串,并切换下一个字符串").onClick(() => {this.index = Math.floor(Math.random() * 5)this.preferences?.put(this.context,this.message[this.index])})}}build() {Column({ space: 30 }) {this.textBuilder()Button("获取持久化字符串列表").onClick(async () => {const list = await this.preferences?.get(this.context)if (list) {this.getUIContext().showAlertDialog({ message: list.toString() })}})}.justifyContent(FlexAlign.Center).width("100%").height("100%")}
}

2、PreferencesUtil.ets

import { preferences } from '@kit.ArkData';/** 偏好设置工具类,用于管理字符串数组类型的偏好数据 */
export class PreferencesUtil {/** 偏好设置数据对象 */private dataPreferences: preferences.Preferences | null = null;/** 上下文对象 */private context: Context | null = null;/** 偏好设置存储名称 */private name: string = 'preferences';/*** 初始化工具类* @param context 上下文对象*/public init(context: Context) {this.context = context;this.dataPreferences = preferences.getPreferencesSync(context, {name: this.name});}/*** 获取存储的字符串数组* @param context 上下文对象* @returns 字符串数组*/public async get(context: Context): Promise<Array<string>> {const pf = await preferences.getPreferences(context, this.name);const data = pf.getSync('my_str', []) as Array<string>;return data;}/*** 添加字符串到数组并保存* @param context 上下文对象* @param str 要添加的字符串*/public async put(context: Context, str: string) {const pf = await preferences.getPreferences(context, this.name);let data = pf.getSync('my_str', []) as Array<string>;data.push(str);pf.putSync('my_str', data);pf.flushSync();}
}/** 单例实例 */
let preferencesUtil: PreferencesUtil | undefined;/*** 获取PreferencesUtil单例* @returns 唯一实例*/
export function getPreferencesUtil(): PreferencesUtil {if (!preferencesUtil) {preferencesUtil = new PreferencesUtil();}return preferencesUtil;
}

以上是个人经验分享。

看完了,如果觉得有所收获,可以免费点个赞或收藏。

http://www.dtcms.com/a/471756.html

相关文章:

  • 在Edge、Chrome浏览器上安装uBlock Origin插件
  • 合肥专业网站制作设计广西最新一批违法领导
  • 湖南建设厅官方网站官网宁波做网站优化哪家好
  • 做谱的网站企业建设网站流程图
  • php网站开发模式有哪些做本地的门户网站
  • 大语言模型与人类、程序的核心区别
  • 什么网站做软文wordpress被屏蔽了api
  • 购物网站一般分几大模块做期货的的都喜欢去什么网站
  • 深入解析PDCP协议:5G与LTE中的核心数据汇聚层
  • 保定网站制作报价网页设计实训报告三个步骤
  • SAM-SAM2-SAM3系列(三):Segment Anything 3(SAM 3)技术详解,以及SAM3对 SAM2的升级点
  • 网站怎么改版自适应如何重新安装电脑上的wordpress
  • 织梦怎么做的网站云南昆明网络公司有哪些
  • 中国建设银行上海分行信息网站广西网站建设企业
  • 房地产公司网站下载wordpress能开发app
  • Codeforces Beta Round 6 C - Alice, Bob and Chocolate
  • 工业大数据处理分析技术
  • 临武县网站建设专业网络推广方案xiala11
  • 河北省 建设执业注册中心网站上海网站企业
  • 搜索引擎优化网站版面设计图大全简单又漂亮
  • 网站建设的财务分险游戏制作专业
  • 政务网站集约化建设难点与建议wordpress首页修改无效
  • 制作网站平台wordpress 文章列表只显示标题
  • 永川区门户网站建设轨迹免费装修设计软件
  • Python第十二节 装饰器使用详解及注意事项
  • 传媒类网站模板做网站怎么赚流量
  • 网站建设 技术方案模板wordpress 外国主机
  • 惠州市博罗县建设局网站双线网站选服务器
  • spring ai用法
  • linux系统服务器怎么做网站外贸网站建设注意事项