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

鸿蒙:PersistenceV2页面间持久化存储数据

前言

PersistenceV2是V2状态管理,本博客展示.connect方法存储数据,用于页面间的持久化数据共享,需要配合使用@ObservedV2@Trace使用。

参考文档

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-new-persistencev2https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-new-persistencev2

核心思路

  1. 将@ObservedV2@Trace修饰的类,通过PersistenceV2.connect绑定到具体的实例上。
  2. 只要修改这个实例的值,UI会更新,持久化也会更新。

运行效果

任一页面点击姓名或年龄,即可全部页面都修改

并且重新运行,依然和最后一次的数据一样,保持不变。

完整代码

1、Index01.ets

import { student, Student, student02, student03 } from './StudentClass';@Entry
@ComponentV2
struct Index01 {@BuilderstudentBuilder(title: string, student: Student) {Column({ space: 10 }) {Text(title)Column({ space: 20 }) {Button(student.name).fontColor(Color.White).fontSize(30).onClick(() => {const name = ["老王", "张三", "王五", "赵六", "小王", "小张",]student.name = name[Math.floor(Math.random() * name.length)]})Button(student.age?.toString()).fontSize(30).fontColor(Color.White).onClick(() => {student.age = student.age! + 1})}.padding(20).width("100%").backgroundColor(Color.Black)}}build() {Column({ space: 20 }) {Text("PersistenceV2 使用示例").fontSize(30).fontWeight(FontWeight.Bold).padding({ bottom: 60 })Button(" 01点击跳转到下一页 ").onClick(() => {this.getUIContext().getRouter().pushUrl({ url: "pages/Index02" })})this.studentBuilder("学生一", student,)this.studentBuilder("学生一", student02,)this.studentBuilder("学生一", student03,)}.justifyContent(FlexAlign.Center).height("100%").width("100%")}
}

2、Indext02.ets

import { student, Student, student02, student03 } from './StudentClass';@Entry
@ComponentV2
struct Index02 {@BuilderstudentBuilder(title: string, student: Student) {Column({ space: 10 }) {Text(title)Column({ space: 20 }) {Button(student.name).fontColor(Color.White).fontSize(30).onClick(() => {const name = ["老王", "张三", "王五", "赵六", "小王", "小张",]student.name = name[Math.floor(Math.random() * name.length)]})Button(student.age?.toString()).fontSize(30).fontColor(Color.White).onClick(() => {student.age = student.age! + 1})}.padding(20).width("100%").backgroundColor(Color.Black)}}build() {Column({ space: 20 }) {Button(" 02 ").onClick(() => {this.getUIContext().getRouter().pushUrl({ url: "pages/Index03" })})this.studentBuilder("学生一", student,)this.studentBuilder("学生一", student02,)this.studentBuilder("学生一", student03,)}.justifyContent(FlexAlign.Center).height("100%").width("100%")}
}

3、Index03.ets

import { student, Student, student02, student03 } from './StudentClass';@Entry
@ComponentV2
struct Index03 {@BuilderstudentBuilder(title: string, student: Student) {Column({ space: 10 }) {Text(title)Column({ space: 20 }) {Button(student.name).fontColor(Color.White).fontSize(30).onClick(() => {const name = ["老王", "张三", "王五", "赵六", "小王", "小张",]student.name = name[Math.floor(Math.random() * name.length)]})Button(student.age?.toString()).fontSize(30).fontColor(Color.White).onClick(() => {student.age = student.age! + 1})}.padding(20).width("100%").backgroundColor(Color.Black)}}build() {Column({ space: 20 }) {Button(" 03 ").onClick(() => {this.getUIContext().getRouter().pushUrl({ url: "pages/Index04" })})this.studentBuilder("学生一", student,)this.studentBuilder("学生一", student02,)this.studentBuilder("学生一", student03,)}.justifyContent(FlexAlign.Center).height("100%").width("100%")}
}

4、StudentClass.ets

import { PersistenceV2 } from "@kit.ArkUI";@ObservedV2
export class Student {@Tracename?: string;@Traceage?: number;constructor() {this.name = "李华";this.age = 21;}
}export const student: Student = PersistenceV2.connect(Student, "student01", () => new Student())!;export const student02: Student = PersistenceV2.connect(Student, "student02", () => new Student())!;export const student03: Student = PersistenceV2.connect(Student, "student03", () => new Student())!;

觉得有用可以点赞或收藏

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

相关文章:

  • wordpress手机版如何在电脑seo广告优化
  • FreeRTOS下STM32双缓冲ADC数据采集与处理
  • 主页导航网站建设定制网站首页菜单栏
  • 校园二手用品网站建设的项目章程南宁网站建设推广优化
  • Docker 容器与镜像
  • 网站ico如何修改有什么做任务得佣金的网站
  • 网站建设背景公司营销策划方案案例
  • 住房和城乡建设部网站主页公司网站建设一条龙
  • 5. 软件工程基础知识
  • C++进阶(2)——多态
  • 营销网站建站开发什么是交换链接
  • 校园风险管理网站建设方案wordpress使用php版本号
  • html头部
  • 韩国网站域名分类常州seo第一人
  • 建设部网站官网办事厅音乐网站素材
  • 人工智能-机器学习day4
  • 网站建设和维护要点重庆cms建站模板
  • 做外汇需要了解的网站网页制作有什么软件
  • 做棋牌网站建设云南网站开发公司找哪家
  • 文案网站策划书织梦网站系统删除
  • Linux开发工具(一)
  • 虚拟资源站码支付wordpress合江县住房建设规划局网站
  • 国企网站建设标准房地产市场发展趋势
  • 网站 用户粘度无人区高清免费看完整版
  • 做数据可视化的网站汕头网站开发找哪里
  • 【MySQL】深分页的性能优化,游标方案和覆盖索引+延迟回表方案
  • 进入深圳市住房和建设局网站胶州市城乡建设局网站
  • 进口倾角传感器代理与水平监测传感器厂家的选择指南
  • 自定义手机网站建设图片分类展示网站源码
  • 基于python数据挖据的教学监控系统的设计与应用