HarmonyOS 鸿蒙Next中状态管理V2,PersistenceV2怎么保存联合类型的数据?
HarmonyOS 鸿蒙Next中状态管理V2,PersistenceV2怎么保存联合类型的数据? 我使用下面的代码,首先点击减少一个,然后点击增加一个A。杀掉后台重新进入,app必闪退,并报错:error key: Storage, reason: serialization, message: Error: The type of target ‘number’ mismatches the type of source ‘string’。
崩溃Log jscrash
Error message:The type mismatches when use the key ‘Storage’ in storage Stacktrace:
- at checkTypeByInstanceOf (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:10763:1)
- at getValueFromDisk (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:11027:1)
- at connect (/usr1/hmos_for_system/src/increment/sourcecode/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:10914:1)
- at connect (…/…/…/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/jsStateManagement.js:98:1)
- at Index (entry/src/main/ets/pages/Index.ets:38:29)
- at anonymous (entry/src/main/ets/pages/Index.ets:118:26)
index.ets
import { PersistenceV2, Type } from "@kit.ArkUI";
interface A{
id: string;
name: string;
}
interface B{
id: number;
job: string;
}
type Types = A | B;
@ObservedV2
export class Storage {
@Trace@Type(Array) lines: Types[] = [
{
id: "1",
name: "1"
},
{
id: 1,
job: "1"
}];
@Trace title: string = "333";
}
// 接受序列化失败的回调
PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => {
console.error(`error key: ${key}, reason: ${reason}, message: ${msg}`);
});
@Entry
@ComponentV2
struct Index {
@Local storage: Storage = PersistenceV2.connect(Storage,"Storage",() => new Storage())!;
aboutToAppear(): void {
console.log(JSON.stringify(this.storage));
}
build() {
Column(){
Text(this.storage.title)
Text(JSON.stringify(this.storage.lines))
Button("变成222").onClick(() => {
this.storage.title = "222";
})
Button("变成333").onClick(() => {
this.storage.title = "333";
})
Button("增加一个A").onClick(()=>{
this.storage.lines.push({
id: "2",
name: "2"
});
})
Button("增加一个B").onClick(()=>{
this.storage.lines.push({
id: 2,
job: "2"
});
})
Button("减少一个").onClick(() => {
this.storage.lines.pop();
})
Button("打印storage").onClick(() => {
console.log(JSON.stringify(this.storage));
})
}.width("100%")
.height("100%")
}
}
更多关于HarmonyOS 鸿蒙Next中状态管理V2,PersistenceV2怎么保存联合类型的数据?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,使用PersistenceV2保存联合类型数据时,需要将其序列化为统一格式。可通过@ObservedV2和@TrackedV2装饰器标记数据类,在PersistenceV2存储时自定义序列化逻辑。
存储时:
- 将联合类型转换为JSON字符串或其他二进制格式
- 使用PersistenceV2的put方法存储
读取时:
- 获取存储的数据字符串
- 根据类型标识反序列化为具体类型
注意处理类型擦除问题,建议在存储时保留类型标识字段。
更多关于HarmonyOS 鸿蒙Next中状态管理V2,PersistenceV2怎么保存联合类型的数据?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中,PersistenceV2目前对联合类型(TypeScript中的Union Types)的支持存在限制。您遇到的问题是由于PersistenceV2在反序列化时无法正确处理联合类型中的不同类型转换。
错误日志显示:“The type of target ‘number’ mismatches the type of source ‘string’”,这表明系统在恢复数据时遇到了类型不一致的问题。
解决方案建议:
-
避免直接使用联合类型作为持久化数据的类型
-
可以改为使用带有类型标识符的单一接口,例如:
interface BaseItem {
type: 'A' | 'B';
}
interface A extends BaseItem {
id: string;
name: string;
}
interface B extends BaseItem {
id: number;
job: string;
}
type Types = A | B;
- 或者考虑将数据序列化为统一格式(如全部转为string)存储,使用时再转换回所需类型
这个问题属于PersistenceV2当前版本的类型系统限制,建议暂时采用上述变通方案处理联合类型数据的持久化需求。