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

鸿蒙:使用AppStorageV2实现跨Ability共享

1、前言

如果你的应用有多个ability,那么该如何在多个ability之间跳转和传输数据呢?很简单,使用AppStorageV2状态管理即可实现。

本篇博客带领大家写个练手的项目,学习体验使用AppStorageV2实现跨Ability共享。

2、参考文档

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-migration-application-and-others#appstorage-appstoragev2https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-migration-application-and-others#appstorage-appstoragev2

3、核心思路

通过AppStorageV2实现不同 Ability 间的数据共享。

  1. 定义MyStorage作为共享数据载体,用@ObservedV2和@Trace标记实现数据监听。
  2. 两个 Ability 的组件均通过AppStorageV2.connect连接到同一'storage'标识的实例,实现数据同步。
  3. 跳转通过startAbility+Want指定目标 Ability,数据传递不依赖 Want 参数,而是通过共享的MyStorage实例实时同步。

      4、核心代码

        @Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!;
           let wantInfo: Want = {bundleName: 'com.example.harmonydemo', // 替换成AppScope/app.json5里的bundleNameabilityName: 'EntryAbility'};this.context.startAbility(wantInfo);

      5、运行效果图

      6、完整代码

      EntryAbility的Index.ets 完整代码:

      import { common, Want } from '@kit.AbilityKit';
      import { AppStorageV2 } from '@kit.ArkUI';@ObservedV2
      export class MyStorage {@Trace count: number = 0
      }@Entry
      @ComponentV2
      struct Index02 {@Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!;private context = this.getUIContext().getHostContext() as common.UIAbilityContext;build() {Column() {Text("不同Ability之间的跳转传参示例").fontWeight(FontWeight.Bold).fontSize(26)Column() {Text(`EntryAbility 数值: ${this.storage.count}`).fontSize(30).onClick(() => {this.storage.count++;})Button('跳转到ApplicationAbility').onClick(() => {try {let wantInfo: Want = {bundleName: 'com.example.harmonydemo', // 替换成AppScope/app.json5里的bundleNameabilityName: 'ApplicationAbility'};this.context.startAbility(wantInfo);} catch (e) {console.error('Index02', 'startAbility failed');}})}.layoutWeight(1).width('100%').alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.Center)}.justifyContent(FlexAlign.SpaceBetween).height('100%')}
      }

      ApplicationAbility的Index.ets 完整代码:

      import { common, Want } from '@kit.AbilityKit';
      import { AppStorageV2 } from '@kit.ArkUI';@ObservedV2
      export class MyStorage {@Trace count: number = 0
      }@Entry
      @ComponentV2
      struct Index {@Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!;private context = this.getUIContext().getHostContext() as common.UIAbilityContext;build() {Column() {Text("不同Ability之间的跳转传参示例").fontSize(26).fontWeight(FontWeight.Bold)Column() {Text(`ApplicationAbility 数值: ${this.storage.count}`).fontSize(30).onClick(() => {this.storage.count++;})Button('跳转到ApplicationAbility').onClick(() => {let wantInfo: Want = {bundleName: 'com.example.harmonydemo', // 替换成AppScope/app.json5里的bundleNameabilityName: 'EntryAbility'};this.context.startAbility(wantInfo);})}.width('100%').layoutWeight(1).justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)}.justifyContent(FlexAlign.SpaceBetween).height('100%')}
      }

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

      相关文章:

    • HarmonyOS SDK地图服务教程
    • C++:(四)类和对象(中)—— 构造、析构与重载
    • 鸿蒙系统权限分级提示设计:兼顾功能需求与用户选择权
    • 【sqllite3】Read error: SQLITE_BUSY: database is locked
    • 做网站建设需要做哪些工作室什么平台可以打广告做宣传
    • Python爬虫实战:获取金价查询网最新金价行情与数据分析
    • 长春网站建设seo推广方式方法
    • 【MySQL✨】MySQL 入门之旅 · 第八篇:数据排序与分组
    • deepseek Kotlin Flow 全面详解
    • MTK-Android13-Dialer 通话界面定制修改
    • 化妆品电子商务网站开发流程描述中山网站建设推荐
    • 宿州移动网站建设广州模板网站
    • 旅游景区网站建设哈尔滨发布信息的网站
    • RVC WebUI(Retrieval-based-Voice-Conversion-WebUI)配置
    • 在线制作简历网站网页结构布局
    • 建网站要备案东莞网站制作品牌祥奔科技
    • 棋盘覆盖问题
    • 大邑网站建设百合居装饰公司官网
    • C++基础(3)-类的6个默认成员函数
    • 做营销型网站需要注意哪些点开发小程序费用
    • AI“点亮”萤火虫:边缘机器学习让微光成像走进4K时代
    • 【手撕机器学习 02】手撕算法的基石:精通NumPy与Pandas向量化思维
    • 一种好用开发的轻量级 Markdown 编辑器
    • 网站用户管理系统徐州市城乡建设局网站
    • 花店网站首页模版帝国cms使用教程
    • React-router v6学生管理系统笔记
    • 手写签名太麻烦?智能签名生成器免费实测 智能签名生成器、智能签名生成器使用、免费电子签名工具、Windows 电子签名软件、办公效率工具
    • 建设银行六安市分行网站hreflang wordpress
    • N8N Workflow Collection - 专业级自动化工作流库
    • 有没有专业做特产的网站小企业如何建网站