目录

@Computed: Declaring Computed Properties

@Computed is a method decorator used in State Management V2 to decorate a getter method, turning it into a computed property. Its return value is cached and recalculated only when the dependent source data changes, reducing the overhead of repeated computation.

For details, see @Computed Decorator: Declaring Computed Properties.

This decorator is supported since API version 12.

@Computed

const Computed: MethodDecorator

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 12.

Widget capability: This API can be used in ArkTS widgets since API version 23.

System capability: SystemCapability.ArkUI.ArkUI.Full

Example:

@Entry
@ComponentV2
struct Index {
  @Local firstName: string = 'Hua';
  @Local lastName: string = 'Li';

  // Declare the computed getter function to avoid repeated calculation.
  @Computed
  get fullName() {
    return this.firstName + ' ' + this.lastName;
  }

  build() {
    Column() {
      Text(`${this.fullName}`)
    }
  }
}