harmony 鸿蒙\@Type Decorator: Marking the Types of the Class Property

  • 2025-06-12
  • 浏览 (6)

\@Type Decorator: Marking the Types of the Class Property

To avoid losing complex types of the properties when serializing classes, you can use the \@Type Decorator to decorate class.

\@Type is used to mark class properties. Used together with PersistenceV2, \@Type can prevent class loss during serialization. Before reading this topic, you are advised to read PersistenceV2.

NOTE

\@Type is supported since API version 12.

Overview

\@Type marks class properties to avoid losing type information during class property serialization, facilitating class deserialization.

Decorator Description

\@Type Decorator Description
Parameter Type.
Allowed type Object class and embedded types such as Array, Date, Map, and Set.

Constraints

  1. \@Type can be used only in classes decorated by \@ObservedV2 and cannot be used in custom components.
class Sample {
  data: number = 0;
}
@ObservedV2
class Info {
  @Type(Sample)
  @Trace sample: Sample = new Sample(); // Correct usage.
}
@Observed
class Info2 {
  @Type(Sample)
  sample: Sample = new Sample(); // Incorrect usage. @Type cannot be used in the @Observed decorated class. Otherwise, an error is reported during compilation.
}
@ComponentV2
struct Index {
  @Type(Sample)
  sample: Sample = new Sample(); // Incorrect usage. @Type cannot be used in the custom component.
  build() {
  }
}
  1. Types such as collections.Set and collections.Map are not supported.

  2. Non-buildin types, such as native PixelMap, NativePointer, and ArrayList types, are not supported.

  3. Simple types such as string, number, and boolean, are not supported.

When to Use

Saving Data for Persistence

Data page

import { Type } from '@kit.ArkUI';

// Data center
@ObservedV2
class SampleChild {
  @Trace p1: number = 0;
  p2: number = 10;
}

@ObservedV2
export class Sample {
  // Complex objects need to be decorated by @Type to ensure successful serialization.
  @Type(SampleChild)
  @Trace f: SampleChild = new SampleChild();
}

Page

import { PersistenceV2 } from '@kit.ArkUI';
import { Sample } from '../Sample';

@Entry
@ComponentV2
struct Page {
  prop: Sample = PersistenceV2.connect(Sample, () => new Sample())!;

  build() {
    Column() {
      Text(`Page1 add 1 to prop.p1: ${this.prop.f.p1}`)
        .fontSize(30)
        .onClick(() => {
          this.prop.f.p1++;
        })
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙\@AnimatableExtend Decorator: Definition of Animatable Attributes

harmony 鸿蒙Application State Management Overview

harmony 鸿蒙AppStorage: Storing Application-wide UI State

harmony 鸿蒙Basic Syntax Overview

harmony 鸿蒙\@Builder Decorator: Custom Builder Function

harmony 鸿蒙\@BuilderParam Decorator: Referencing the \@Builder Function

harmony 鸿蒙Creating a Custom Component

harmony 鸿蒙Mixing Use of Custom Components

harmony 鸿蒙Constraints on Access Modifiers of Custom Component Member Variables

harmony 鸿蒙Freezing a Custom Component

0  赞