harmony 鸿蒙\@ComponentV2 Decorator: Custom Component

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

\@ComponentV2 Decorator: Custom Component

To use the capability of the state variable decorator V2 in a custom component, you can use the \@ComponentV2 Decorator to decorate the custom component.

\@ComponentV2 is used together with state management V2. Before reading this topic, you are advised to read State Management Overview.

NOTE

The \@ComponentV2 decorator is supported since API version 12.

Overview

Similar to \@Component Decorator, the \@ComponentV2 decorator is used to decorate custom components.

  • In the custom component decorated by \@ComponentV2, only new state variable decorators can be used, including \@Local, \@Param, \@Once, \@Event, \@Provider, and \@Consumer.

  • Custom components decorated by \@ComponentV2 do not support component reuse and LocalStorage.

  • \@ComponentV2 and \@Component cannot be used to decorate the same struct.

  • \@ComponentV2 can use an optional parameter freezeWhenInactive of the boolean type to implement the component freezing.

  • A simple \@ComponentV2 decorated custom component should contain the following parts:

    @ComponentV2 // Decorator
    struct Index { // Data declared by the struct
      build() { // UI defined by build
      }
    }
    

Unless otherwise specified, a custom component decorated by \@ComponentV2 maintains the same behavior as a custom component decorated by \@Component.

Constraints

For details about how to use the custom components decorated by \@Component and \@ComponentV2 together, see Mixing Use of Custom Components.

你可能感兴趣的鸿蒙文章

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  赞