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 422. @State、@Provide、 @Link和@Consume四种状态变量的数据类型声明只能由简单数据类型或引用数据类型的其中一种构成。 43 44 类型定义中的Length、ResourceStr、ResourceColor三个类型是简单数据类型或引用数据类型的组合,所以不能被以上四种状态装饰器变量使用。 45 Length、ResourceStr、ResourceColor的定义请看文档[arkui-ts类型定义](../../../application-dev/reference/arkui-ts/ts-types.md)。 46 47 示例: 48 49 ```ts 50 // xxx.ets 51 @Entry 52 @Component 53 struct IndexPage { 54 // 错误写法: @State message: string | Resource = 'Hello World' 55 @State message: string = 'Hello World' 56 // 错误写法: @State message: ResourceStr = $r('app.string.hello') 57 @State resourceStr: Resource = $r('app.string.hello') 58 59 build() { 60 Row() { 61 Column() { 62 Text(`${this.message}`) 63 .fontSize(50) 64 .fontWeight(FontWeight.Bold) 65 } 66 .width('100%') 67 } 68 .height('100%') 69 } 70 } 71 ``` 72 73  74 75**变更影响** 76 771. 如果状态装饰器变量没有显式声明变量类型,声明any,编译拦截报错; 78 ```ts 79 // ArkTS:ERROR Please define an explicit type, not any. 80 @State isLunar: any = false 81 ``` 822. 状态装饰器变量声明变量类型为Date,编译拦截报错; 83 ```ts 84 // ArkTS:ERROR The @State property 'selectedDate' cannot be a 'Date' object. 85 @State selectedDate: Date = new Date('2021-08-08') 86 ``` 873. @State、@Provide、 @Link和@Consume四种状态变量使用框架提供的Length、ResourceStr、ResourceColor, 88 编译拦截报错。 89 ```ts 90 /* ArkTS:ERROR The state variable type here is 'ResourceStr', it contains both a simple type and an object type, 91 which are not allowed to be defined for state variable of a struct.*/ 92 @State message: ResourceStr = $r('app.string.hello') 93 ``` 94 95**关键的接口/组件变更** 96 97不涉及。 98 99**适配指导** 100 1011. 状态装饰器变量声明具体的变量类型替代any; 1022. 使用Date对象的状态装饰器变量,修改为不加状态装饰器修饰的常规变量; 1033. 因为Length(string|number|Resource), ResourceStr(string|Resource), ResourceColor(string|number|Color|Resource) 104 的三个类型是简单数据类型或引用数据类型的组合,使用@State、@Provide、 @Link和@Consume四种状态变量场景参考以下修改: 105 ```ts 106 // 错误写法: 107 @State message: ResourceStr = $r('app.string.hello') 108 // 修正后的写法: 109 @State resourceStr: Resource = $r('app.string.hello') 110 ``` 111 112 113## cl.arkui.2 自定义组件成员变量初始化的规则与约束 114 115通过构造函数方法初始化成员变量,需要遵循如下规则: 116 117| **从父组件中的变量(右)到子组件中的变量(下)** | **regular** | **@State** | **@Link** | **@Prop** | **@Provide** | **@Consume** | **@ObjectLink** | 118|---------------------------------|----------------------------|------------|-----------|-----------|--------------|--------------|------------------| 119| **regular** | 支持 | 支持 | 支持 | 支持 | 不支持 | 不支持 | 支持 | 120| **@State** | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 121| **@Link** | 不支持 | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 122| **@Prop** | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 123| **@Provide** | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 支持 | 124| **@Consume** | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 125| **@ObjectLink** | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 不支持 | 126 127| **从父组件中的变量(右)到子组件中的变量(下)** | **@StorageLink** | **@StorageProp** | **@LocalStorageLink** | **@LocalStorageProp** | 128|------------------|------------------|------------------|-----------------------|------------------------| 129| **regular** | 支持 | 不支持 | 不支持 | 不支持 | 130| **@State** | 支持 | 支持 | 支持 | 支持 | 131| **@Link** | 支持(1) | 支持(1) | 支持(1) | 支持(1) | 132| **@Prop** | 支持 | 支持 | 支持 | 支持 | 133| **@Provide** | 支持 | 支持 | 支持 | 支持 | 134| **@Consume** | 不支持 | 不支持 | 不支持 | 不支持 | 135| **@ObjectLink** | 不支持 | 不支持 | 不支持 | 不支持 | 136 137> **说明** 138> 139> **支持(1)**:必须使用`$`, 例如 `this.$varA`。 140> **regular**:未加修饰的常规变量。 141 142不允许从父组件初始化`@StorageLink`, `@StorageProp`, `@LocalStorageLink`, `@LocalStorageProp`修饰的变量。 143 144**变更影响** 145 1461. 不允许从父组件初始化`@LocalStorageLink`, `@LocalStorageProp`修饰的变量。 147 ```ts 148 @Entry 149 @Component 150 struct LocalStorageComponent { 151 build() { 152 Column() { 153 Child({ 154 /* ArkTS:ERROR Property 'simpleVarName' in the custom component 'Child' cannot 155 initialize here (forbidden to specify). */ 156 simpleVarName: 1, 157 /* ArkTS:ERROR Property 'objectName' in the custom component 'Child' cannot 158 initialize here (forbidden to specify). */ 159 objectName: new ClassA("x") 160 }) 161 } 162 } 163 } 164 @Component 165 struct Child { 166 @LocalStorageLink("storageSimpleProp") simpleVarName: number = 0; 167 @LocalStorageProp("storageObjectProp") objectName: ClassA = new ClassA("x"); 168 build() {} 169 } 170 ``` 1712. 子组件的@ObjectLink变量不支持父组件装饰器变量的直接赋值,其父组件的源必须是数组的项或对象的属性,该数组或对象必现用`@State`、`@Link`、`@Provide`、`@Consume`或`@ObjectLink`装饰器修饰。 172 ```ts 173 let NextID : number = 0; 174 175 @Observed class ClassA { 176 public id : number; 177 public c: number; 178 constructor(c: number) { 179 this.id = NextID++; 180 this.c = c; 181 } 182 } 183 184 @Component 185 struct Child { 186 @ObjectLink varA : ClassA; 187 build() { 188 Row() { 189 Text('ViewA-' + this.varA.id) 190 } 191 } 192 } 193 194 @Component 195 struct Parent { 196 @Link linkValue: ClassA 197 build() { 198 Column() { 199 /* ArkTS:ERROR The @Link property 'linkValue' cannot be assigned to 200 the @ObjectLink property 'varA'.*/ 201 Child({ varA: this.linkValue }) 202 } 203 } 204 } 205 ``` 206 207**关键的接口/组件变更** 208 209不涉及。 210 211**适配指导** 2121. 构造子组件时,不对子组件的`@LocalStorageLink`, `@LocalStorageProp`修饰的变量进行。 213如果需要在父组件中修改子组件的`@LocalStorageLink`, `@LocalStorageProp`修饰的变量,则使用LocalStorage提供的API接口方法(比如set方法)赋值。 2142. @ObjectLink的使用指导请参考文档@ObjectLink使用指导。 215 216 217## cl.arkui.LocalStorage.1 get接口返回类型变更 218 219**变更影响** 220 221返回类型从get<T>(propName: string): T变更为get<T>(propName: string): T | undefined 222应用不需要进行适配。 223 224## cl.arkui.LocalStorage.2 setOrCreate参数newValue变成必选 225**变更影响** 226 227原接口声明: 228```js 229setOrCreate<T>(propName: string, newValue?: T): boolean 230``` 231现接口声明: 232```js 233setOrCreate<T>(propName: string, newValue: T): boolean 234``` 235第二个参数newValue变为必选。 236如果应用调用这个接口没有指定newValue参数,在替换新的sdk后会编译不过,需要手动指定newValue。 237 238**适配指导** 239 240```js 241let storage = new LocalStorage(); 242storage.setOrCreate('propA', 'hello'); 243``` 244## cl.arkui.LocalStorage.3 link参数和返回类型变更 245**变更影响** 246 247原接口声明: 248```js 249link<T>(propName: string, linkUser?: T, subscribersName?: string): T 250``` 251现接口声明: 252```js 253link<T>(propName: string): SubscribedAbstractProperty<T> 254``` 2551. link第二三个参数为框架内部调用,不应对外开发,所以将接口变更为一个参数; 2562. 返回类型T变更为SubscribedAbstractProperty; 257 258**适配指导** 259 260```js 261let storage = new LocalStorage({"PropA": "47"}); 262let linA = storage.link("PropA"); 263linA.set(50); 264``` 265 266## cl.arkui.LocalStorage.4 setAndLink参数和返回类型变更 267**变更影响** 268 269原接口声明: 270```js 271setAndLink<T>(propName: string, defaultValue: T, linkUser?: T, subscribersName?: string): T 272``` 273现接口声明: 274```js 275setAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T> 276``` 2771. setAndLink第三四个参数为框架内部调用,不应对外开发,所以将接口变更为2个参数; 2782. 返回类型T变更为SubscribedAbstractProperty; 279 280**适配指导** 281 282```js 283let storage = new LocalStorage({"PropA": "47"}); 284let linA = storage.setAndLink("PropA", "48") 285linA.set(50); 286``` 287 288## cl.arkui.LocalStorage.5 prop参数和返回类型变更 289**变更影响** 290 291原接口声明: 292```js 293prop<T>(propName: string, propUser?: T, subscribersName?: string): T 294``` 295现接口声明: 296```js 297prop<S>(propName: string): SubscribedAbstractProperty<S> 298``` 2991. prop第二三个参数为框架内部调用,不应对外开发,所以将接口变更为1个参数; 3002. 返回类型T变更为SubscribedAbstractProperty; 301 302**适配指导** 303 304```js 305let storage = new LocalStorage({"PropA": "47"}); 306let propA = storage.prop("PropA"); 307propA.set(51); // one-way sync 308``` 309 310## cl.arkui.LocalStorage.6 setAndProp参数和返回类型变更 311**变更影响** 312 313原接口声明: 314```js 315setAndProp<T>(propName: string, defaultValue: T, propUser?: T, subscribersName?: string): T 316``` 317现接口声明: 318```js 319setAndProp<S>(propName: string, defaultValue: S): SubscribedAbstractProperty<S> 320``` 3211. setAndProp第三四个参数为框架内部调用,不应对外开发,所以将接口变更为2个参数; 3222. 返回类型T变更为SubscribedAbstractProperty; 323 324**适配指导** 325 326```js 327let storage = new LocalStorage({"PropA": "47"}); 328let propA = storage.setAndProp("PropA", "48"); 329propA.set(51); // one-way sync 330``` 331