开发者技术支持-AppStorage与AppStorageV2之间转化
【摘要】 1.问题说明:在开发中可能会遇到这样的情景,以前的项目是用V1写的,由于某种需要,需要转成V2;其中在处理跨页面数据共享这块儿,在V1中通常使用的是AppStorage,而V2中没有AppStorage。这个时候若想实现这种功能,可以选用AppStorageV2。2.原因分析:在V1与V2的转化过程中遇到过这种情况,V1的写法是本地的修改不同步AppStorage,但AppStorage的变...
1.问题说明:在开发中可能会遇到这样的情景,以前的项目是用V1写的,由于某种需要,需要转成V2;其中在处理跨页面数据共享这块儿,在V1中通常使用的是AppStorage,而V2中没有AppStorage。这个时候若想实现这种功能,可以选用AppStorageV2。
2.原因分析:在V1与V2的转化过程中遇到过这种情况,V1的写法是本地的修改不同步AppStorage,但AppStorage的变化能够通知到使用@StorageProp装饰器的组件;如下:
import { common, Want } from '@kit.AbilityKit'; @Entry @Component struct Index { @StorageProp('count') count: number = 0; private context = this.getUIContext().getHostContext() as common.UIAbilityContext; build() { Column() { Text(`EntryAbility count: ${this.count}`) .fontSize(25) .onClick(() => { this.count++; }) Button('change Storage Count') .onClick(() => { AppStorage.setOrCreate('count', AppStorage.get<number>('count') as number + 100); }) Button('Jump to EntryAbility1').onClick(() => { let wantInfo: Want = { bundleName: 'com.example.myapplication', // 替换成AppScope/app.json5里的bundleName abilityName: 'EntryAbility1' }; this.context.startAbility(wantInfo); }) } } } // EntryAbility1 Index1.ets import { common, Want } from '@kit.AbilityKit'; @Entry @Component struct Index1 { @StorageProp('count') count: number = 0; private context = this.getUIContext().getHostContext() as common.UIAbilityContext; build() { Column() { Text(`EntryAbility1 count: ${this.count}`) .fontSize(50) .onClick(() => { this.count++; }) Button('change Storage Count') .onClick(() => { AppStorage.setOrCreate('count', AppStorage.get<number>('count') as number + 100); }) Button('Jump to EntryAbility').onClick(() => { let wantInfo: Want = { bundleName: 'com.example.myapplication', // 替换成AppScope/app.json5里的bundleName abilityName: 'EntryAbility' }; this.context.startAbility(wantInfo); }) } } }
这是V1的写法,我们需要把这种写法转成V2,这个时候就要用AppStorageV2进行替代
3.解决思路:
虽然V2中没有@StorageProp,但是我们可以使用@Monitor和@Local实现类似效果
“@StorageProp('count') count: number = 0;”这种写法可以用@Monitor和@Local进行替换。
首先创建一个对象:
@ObservedV2 export class MyStorage { @Trace count: number = 0; }
接着
@Entry @ComponentV2 struct Index { @Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!; @Local count: number = this.storage.count; private context = this.getUIContext().getHostContext() as common.UIAbilityContext; @Monitor('storage.count') onCountChange(mon: IMonitor) { console.info(`Index1 ${mon.value()?.before} to ${mon.value()?.now}`); this.count = this.storage.count; } build() { Column() { Text(`EntryAbility1 count: ${this.count}`) .fontSize(25) .onClick(() => { this.count++; }) Button('change Storage Count') .onClick(() => { this.storage.count += 100; }) Button('Jump to EntryAbility1').onClick(() => { let wantInfo: Want = { bundleName: 'com.example.myapplication', // 替换成AppScope/app.json5里的bundleName abilityName: 'EntryAbility1' }; this.context.startAbility(wantInfo); }) } } }
import { common, Want } from '@kit.AbilityKit'; import { AppStorageV2 } from '@kit.ArkUI'; @ObservedV2 export class MyStorage { @Trace count: number = 0; } @Entry @ComponentV2 struct Index1 { @Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!; @Local count: number = this.storage.count; private context = this.getUIContext().getHostContext() as common.UIAbilityContext; @Monitor('storage.count') onCountChange(mon: IMonitor) { console.info(`Index1 ${mon.value()?.before} to ${mon.value()?.now}`); this.count = this.storage.count; } build() { Column() { Text(`EntryAbility1 count: ${this.count}`) .fontSize(25) .onClick(() => { this.count++; }) Button('change Storage Count') .onClick(() => { this.storage.count += 100; }) Button('Jump to EntryAbility').onClick(() => { let wantInfo: Want = { bundleName: 'com.example.myapplication', // 替换成AppScope/app.json5里的bundleName abilityName: 'EntryAbility' }; this.context.startAbility(wantInfo); }) } } }
4.解决方案:
如上,解决这个过程,只需要把
@StorageProp('count') count: number = 0;
转化为
- @Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!;
- @Local count: number = this.storage.count;
- private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
- @Monitor('storage.count')
- onCountChange(mon: IMonitor) {
- console.info(`Index1 ${mon.value()?.before} to ${mon.value()?.now}`);
- this.count = this.storage.count;
- }
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)