HarmonyOS ets替换ts编译报错问题?

下面的场景改为ets该如何实现:

1、通过JSON反序列化成指定对象。

Object.assign(location, JSON.parse(localCache))

因禁用了assign使用,如何转成指定类型的对象?

2、通过Object.fromEntries把map转为Object。

let lbsDataJson = JSON.stringify(Object.fromEntries(map));

3、通过Object.entries把Object转为map的场景。

阅读 455
1 个回答

JSON.parse(localCacheJson) //转换出来是Object类型,在调用对应方法时找不到,所以需要转为指定HllLocation类型。请参考如下文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkts-61-V5

参考示例:

class HllLocation {
  name: string
  age: number
  email: string
  phoneNumber: string

  constructor(name: string, age: number, email: string,
    phoneNumber: string) {
    this.name = name;
    this.age = age;
    this.email = email;
    this.phoneNumber = phoneNumber;
  }
}

let test10: HllLocation = new HllLocation('1', 2, '1', '1')

function assign(target: Object, ...source: Object[]): Object {
  for (const items of source) {
    for (const key of Object.keys(items)) {
      (target as Record<string, Object>)[key] = Reflect.get(items, key)
    }
  }
  return target;
}

let a = assign(test10) as HllLocation
logo
HarmonyOS
子站问答
访问
宣传栏