HarmonyOS 鸿蒙Next arkts如何序列化一个对象
HarmonyOS 鸿蒙Next arkts如何序列化一个对象
const Serializable = (obj: Ms.ClientInfo) => {
return Object.keys(obj).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`).join('&');
};
提示Indexed access is not supported for fields (arkts-no-props-by-index)
3 回复
Reflect获取value值
Serializable(obj: Person) {
return Object.keys(obj).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(Reflect.get(obj, key))}`).join('&');
};
使用Object() 将 class 转 object
class ClientInfo {
name: string
age: number
sex: string
constructor(name: string, age: number, sex: string) {
this.name = name
this.age = age
this.sex = sex
}
}
[@Entry](/user/Entry)
[@Component](/user/Component)
struct HitTestBehaviorExample {
build() {
// outer stack
Column() {
Button('测试')
.onClick(() => {
const Serializable = (obj: ClientInfo) => {
return Object.keys(obj)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(Object(obj)[key])}`)
.join('&');
};
console.info(`Serializable:${Serializable(new ClientInfo("张三", 30, "女"))}`)
})
}.width('100%').height('100%')
}
}