1# arkui子系统ChangeLog 2 3## cl.arkui.1 状态变量数据类型声明使用限制。 4 51. 所有的状态装饰器变量需要显式声明变量类型,不允许声明any,不支持Date数据类型。 6 7 示例: 8 9 ```ts 10 // xxx.ets 11 @Entry 12 @Component 13 struct DatePickerExample { 14 // 错误写法: @State isLunar: any = false 15 @State isLunar: boolean = false 16 // 错误写法: @State selectedDate: Date = new Date('2021-08-08') 17 private selectedDate: Date = new Date('2021-08-08') 18 19 build() { 20 Column() { 21 Button('切换公历农历') 22 .margin({ top: 30 }) 23 .onClick(() => { 24 this.isLunar = !this.isLunar 25 }) 26 DatePicker({ 27 start: new Date('1970-1-1'), 28 end: new Date('2100-1-1'), 29 selected: this.selectedDate 30 }) 31 .lunar(this.isLunar) 32 .onChange((value: DatePickerResult) => { 33 this.selectedDate.setFullYear(value.year, value.month, value.day) 34 console.info('select current date is: ' + JSON.stringify(value)) 35 }) 36 37 }.width('100%') 38 } 39 } 40 ``` 41 42  43 442. @State、@Provide、 @Link和@Consume四种状态变量的数据类型声明只能由简单数据类型或引用数据类型的其中一种构成。 45 46 类型定义中的Length、ResourceStr、ResourceColor三个类型是简单数据类型或引用数据类型的组合,所以不能被以上四种状态装饰器变量使用。 47 Length、ResourceStr、ResourceColor的定义请看文档[arkui-ts类型定义](../../../application-dev/reference/arkui-ts/ts-types.md)。 48 49 示例: 50 51 ```ts 52 // xxx.ets 53 @Entry 54 @Component 55 struct IndexPage { 56 // 错误写法: @State message: string | Resource = 'Hello World' 57 @State message: string = 'Hello World' 58 // 错误写法: @State message: ResourceStr = $r('app.string.hello') 59 @State resourceStr: Resource = $r('app.string.hello') 60 61 build() { 62 Row() { 63 Column() { 64 Text(`${this.message}`) 65 .fontSize(50) 66 .fontWeight(FontWeight.Bold) 67 } 68 .width('100%') 69 } 70 .height('100%') 71 } 72 } 73 ``` 74 75  76 77**变更影响** 78 791. 如果状态装饰器变量没有显式声明变量类型,声明any,编译拦截报错; 80 ```ts 81 // ArkTS:ERROR Please define an explicit type, not any. 82 @State isLunar: any = false 83 ``` 842. 状态装饰器变量声明变量类型为Date,编译拦截报错; 85 ```ts 86 // ArkTS:ERROR The @State property 'selectedDate' cannot be a 'Date' object. 87 @State selectedDate: Date = new Date('2021-08-08') 88 ``` 893. @State、@Provide、 @Link和@Consume四种状态变量使用框架提供的Length、ResourceStr、ResourceColor, 90 编译拦截报错。 91 ```ts 92 /* ArkTS:ERROR The state variable type here is 'ResourceStr', it contains both a simple type and an object type, 93 which are not allowed to be defined for state variable of a struct.*/ 94 @State message: ResourceStr = $r('app.string.hello') 95 ``` 96 97**关键的接口/组件变更** 98 99不涉及。 100 101**适配指导** 102 1031. 状态装饰器变量声明具体的变量类型替代any; 1042. 使用Date对象的状态装饰器变量,修改为不加状态装饰器修饰的常规变量; 1053. 因为Length(string|number|Resource), ResourceStr(string|Resource), ResourceColor(string|number|Color|Resource) 106 的三个类型是简单数据类型或引用数据类型的组合,使用@State、@Provide、 @Link和@Consume四种状态变量场景参考以下修改: 107 ```ts 108 // 错误写法: 109 @State message: ResourceStr = $r('app.string.hello') 110 // 修正后的写法: 111 @State resourceStr: Resource = $r('app.string.hello') 112 ``` 113 114## cl.arkui.2 自定义组件成员变量初始化的规则与约束。 115 116通过构造函数方法初始化成员变量,需要遵循如下规则: 117 118| **从父组件中的变量(右)到子组件中的变量(下)** | **regular** | **@State** | **@Link** | **@Prop** | **@Provide** | **@Consume** | **@ObjectLink** | 119| -------------------------- | ----------- | ---------- | --------- | --------- | ------------ | ------------ | --------------- | 120| **regular** | 支持 | 支持 | 支持 | 支持 | 不支持 | 不支持 | 支持 | 121| **@State** | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 122| **@Link** | 不支持 | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 123| **@Prop** | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 124| **@Provide** | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 125| **@Consume** | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 126| **@ObjectLink** | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 127 128| **从父组件中的变量(右)到子组件中的变量(下)** | **@StorageLink** | **@StorageProp** | **@LocalStorageLink** | **@LocalStorageProp** | 129| -------------------------- | ---------------- | ---------------- | --------------------- | --------------------- | 130| **regular** | 支持 | 不支持 | 不支持 | 不支持 | 131| **@State** | 支持 | 支持 | 支持 | 支持 | 132| **@Link** | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 133| **@Prop** | 支持 | 支持 | 支持 | 支持 | 134| **@Provide** | 支持 | 支持 | 支持 | 支持 | 135| **@Consume** | 不支持 | 不支持 | 不支持 | 不支持 | 136| **@ObjectLink** | 不支持 | 不支持 | 不支持 | 不支持 | 137 138> **说明** 139> 140> **支持(1)**:必须使用`$`, 例如 `this.$varA`。 141> **regular**:未加修饰的常规变量。 142 143不允许从父组件初始化`@StorageLink`, `@StorageProp`, `@LocalStorageLink`, `@LocalStorageProp`修饰的变量。 144 145**变更影响** 146 1471. 不允许从父组件初始化`@LocalStorageLink`, `@LocalStorageProp`修饰的变量。 148 ```ts 149 @Entry 150 @Component 151 struct LocalStorageComponent { 152 build() { 153 Column() { 154 Child({ 155 /* ArkTS:ERROR Property 'simpleVarName' in the custom component 'Child' cannot 156 initialize here (forbidden to specify). */ 157 simpleVarName: 1, 158 /* ArkTS:ERROR Property 'objectName' in the custom component 'Child' cannot 159 initialize here (forbidden to specify). */ 160 objectName: new ClassA("x") 161 }) 162 } 163 } 164 } 165 @Component 166 struct Child { 167 @LocalStorageLink("storageSimpleProp") simpleVarName: number = 0; 168 @LocalStorageProp("storageObjectProp") objectName: ClassA = new ClassA("x"); 169 build() {} 170 } 171 ``` 1722. 子组件的@ObjectLink变量不支持父组件装饰器变量的直接赋值,其父组件的源必须是数组的项或对象的属性,该数组或对象必现用`@State`、`@Link`、`@Provide`、`@Consume`或`@ObjectLink`装饰器修饰。 173 ```ts 174 let NextID : number = 0; 175 176 @Observed class ClassA { 177 public id : number; 178 public c: number; 179 constructor(c: number) { 180 this.id = NextID++; 181 this.c = c; 182 } 183 } 184 185 @Component 186 struct Child { 187 @ObjectLink varA : ClassA; 188 build() { 189 Row() { 190 Text('ViewA-' + this.varA.id) 191 } 192 } 193 } 194 195 @Component 196 struct Parent { 197 @Link linkValue: ClassA 198 build() { 199 Column() { 200 /* ArkTS:ERROR The @Link property 'linkValue' cannot be assigned to 201 the @ObjectLink property 'varA'.*/ 202 Child({ varA: this.linkValue }) 203 } 204 } 205 } 206 ``` 207 208**关键的接口/组件变更** 209 210不涉及。 211 212**适配指导** 2131. 构造子组件时,不对子组件的`@LocalStorageLink`, `@LocalStorageProp`修饰的变量进行。 214 如果需要在父组件中修改子组件的`@LocalStorageLink`, `@LocalStorageProp`修饰的变量,则使用LocalStorage提供的API接口方法(比如set方法)赋值。 2152. @ObjectLink的使用指导请参考文档[@ObjectLink使用指导](../../../application-dev/quick-start/arkts-observed-and-objectlink.md)。 216