ts-custom-component-initialization.md 4.2 KB
Newer Older
G
ge-yafang 已提交
1
# Initialization of Custom Components' Member Variables
Z
zengyawen 已提交
2 3 4 5 6


The member variables of a component can be initialized in either of the following ways:


G
ge-yafang 已提交
7 8 9 10 11
- Local initialization. For example:
    
  ```
  @State counter: Counter = new Counter()
  ```
Z
zengyawen 已提交
12

G
ge-yafang 已提交
13 14 15 16 17
- Initialization using constructor parameters. For example:
    
  ```
  MyComponent(counter: $myCounter)
  ```
Z
zengyawen 已提交
18 19


Z
zengyawen 已提交
20
The allowed method depends on the decorator of the state variable, as shown in the following table.
Z
zengyawen 已提交
21

G
ge-yafang 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34

  | Decorator Type | Local Initialization | Initialization Using Constructor Parameters | 
| -------- | -------- | -------- |
| @State | Mandatory | Optional | 
| @Prop | Forbidden | Mandatory | 
| @Link | Forbidden | Mandatory | 
| @StorageLink | Mandatory | Forbidden | 
| @StorageProp | Mandatory | Forbidden | 
| @Provide | Mandatory | Optional | 
| @Consume | Forbidden | Forbidden | 
| @ObjectLink | Forbidden | Mandatory | 
| Normal member variable | Recommended | Optional | 

Z
zengyawen 已提交
35

Z
zengyawen 已提交
36
As indicated by the preceding table:
Z
zengyawen 已提交
37

G
ge-yafang 已提交
38 39 40 41 42

- The @State decorated variables need to be initialized locally. The initial value can be overwritten by the constructor parameter.

- The @Prop and @Link decorated variables must be initialized only by constructor parameters.

Z
zengyawen 已提交
43 44 45

Comply with the following rules when using constructors to initialize member variables:

G
ge-yafang 已提交
46 47 48 49 50 51 52 53 54 55

  | From the Variable in the Parent Component (Below) to the Variable in the Child Component (Right) | @State | @Link | @Prop | Normal Variable | 
| -------- | -------- | -------- | -------- | -------- |
| @State | Not allowed | Allowed | Allowed | Allowed | 
| @Link | Not allowed | Allowed | Not recommended | Allowed | 
| @Prop | Not allowed | Not allowed | Allowed | Allowed | 
| @StorageLink | Not allowed | Allowed | Not allowed | Allowed | 
| @StorageProp | Not allowed | Not allowed | Not allowed | Allowed | 
| Normal variable | Allowed | Not allowed | Not allowed | Allowed | 

Z
zengyawen 已提交
56

Z
zengyawen 已提交
57
As indicated by the preceding table:
Z
zengyawen 已提交
58 59


G
ge-yafang 已提交
60
- The normal variables of the parent component can be used to initialize the @State decorated variables of the child component, but not the @Link or @Prop decorated variables.
Z
zengyawen 已提交
61

G
ge-yafang 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75
- The @State decorated variable of the parent component can be used to initialize the @Prop, @Link (using $), or normal variables of the child component, but not the @State decorated variables of the child component.

- The @Link decorated variables of the parent component can be used to initialize the @Link decorated or normal variables of the child component. However, initializing the @State decorated members of the child component can result in a syntax error. In addition, initializing the @Prop decorated variables is not recommended.

- The @Prop decorated variables of the parent component can be used to initialize the normal variables or @Prop decorated variables of the child component, but not the @State or @Link decorated variables.

- Passing @StorageLink and @StorageProp from the parent component to the child component is prohibited.

- In addition to the preceding rules, the TypeScript strong type rules need to be followed.


## Example

  
Z
zengyawen 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
```
@Entry
@Component
struct Parent {
    @State parentState: ClassA = new ClassA()
    build() {
        Row() {
            CompA({aState: new ClassA, aLink: $parentState}) // valid
            CompA({aLink: $parentState})   // valid
            CompA()                 // invalid, @Link aLink remains uninitialized
            CompA({aLink: new ClassA}) // invalid, @Link aLink must be a reference ($) to either @State or @Link variable
        }
    }
}

@Component
struct CompA {
    @State aState: boolean = false   // must initialize locally
    @Link aLink: ClassA              // must not initialize locally

    build() {
        Row() {
            CompB({bLink: $aLink,         // valid init a @Link with reference of another @Link,
                bProp: this.aState})    // valid init a @Prop with value of a @State
N
Neil Chen 已提交
100
            CompB({aLink: $aState,  // invalid: type mismatch expected ref to ClassA, provided reference to boolean
Z
zengyawen 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
                bProp: false})           // valid init a @Prop by constants value
        }
    }
}

@Component
struct CompB {
    @Link bLink: ClassA = new ClassA()       // invalid, must not initialize locally
    @Prop bProp: boolean = false      // invalid must not initialize locally

    build() {
        ...
    }
}
```