diff --git a/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md b/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md index 7df90501a582c02e796a5eb61a73013dc14335ee..d63149dcb8418d6d5bf6028dcbeb074f1b1ff1fa 100644 --- a/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md +++ b/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md @@ -35,7 +35,7 @@ | ----------------- | ---------------------------------------- | | 装饰器参数 | 无 | | 同步类型 | 不与父组件中的任何类型同步变量。 | -| 允许装饰的变量类型 | 必须为被\@Observed装饰的class实例,必须指定类型。
不支持简单类型,可以使用[\@Prop](arkts-prop.md)。
\@ObjectLink的属性是可以改变的,但是变量的分配是不允许的,也就是说这个装饰器装饰变量是只读的,不能被改变。 | +| 允许装饰的变量类型 | 必须为被\@Observed装饰的class实例,必须指定类型。
不支持简单类型,可以使用[\@Prop](arkts-prop.md)。
支持继承Date或者Array的class实例,示例见[观察变化](#观察变化)。
\@ObjectLink的属性是可以改变的,但是变量的分配是不允许的,也就是说这个装饰器装饰变量是只读的,不能被改变。 | | 被装饰变量的初始值 | 不允许。 | \@ObjectLink装饰的数据为可读示例。 @@ -75,7 +75,7 @@ this.objLink= ... ## 观察变化和行为表现 -### 观察的变化 +### 观察变化 \@Observed装饰的类,如果其属性为非简单类型,比如class、Object或者数组,也需要被\@Observed装饰,否则将观察不到其属性的变化。 @@ -121,6 +121,67 @@ this.b.a.c = 5 - 如果数据源是数组,则可以观察到数组item的替换,如果数据源是class,可观察到class的属性的变化,示例请参考[对象数组](#对象数组)。 +继承Date的class时,可以观察到Date整体的赋值,同时可通过调用Date的接口`setFullYear`, `setMonth`, `setDate`, `setHours`, `setMinutes`, `setSeconds`, `setMilliseconds`, `setTime`, `setUTCFullYear`, `setUTCMonth`, `setUTCDate`, `setUTCHours`, `setUTCMinutes`, `setUTCSeconds`, `setUTCMilliseconds` 更新Date的属性。 + +```ts +@Observed +class DateClass extends Date { + constructor(args: any) { + super(args) + } +} + +@Observed +class ClassB { + public a: DateClass; + + constructor(a: DateClass) { + this.a = a; + } +} + +@Component +struct ViewA { + label: string = 'date'; + @ObjectLink a: DateClass; + + build() { + Column() { + Button(`child increase the day by 1`) + .onClick(() => { + this.a.setDate(this.a.getDate() + 1); + }) + DatePicker({ + start: new Date('1970-1-1'), + end: new Date('2100-1-1'), + selected: this.a + }) + } + } +} + +@Entry +@Component +struct ViewB { + @State b: ClassB = new ClassB(new DateClass('2023-1-1')); + + build() { + Column() { + ViewA({ label: 'date', a: this.b.a }) + + Button(`parent update the new date`) + .onClick(() => { + this.b.a = new DateClass('2023-07-07'); + }) + Button(`ViewB: this.b = new ClassB(new DateClass('2023-08-20'))`) + .onClick(() => { + this.b = new ClassB(new DateClass('2023-08-20')); + }) + } + } +} +``` + ### 框架行为 diff --git a/zh-cn/application-dev/reference/arkui-ts/ts-state-management.md b/zh-cn/application-dev/reference/arkui-ts/ts-state-management.md index 6fe560c25eaca837892b1c8e88903b09151fc387..5a4f8f6383ccf1d17788291801b5721fb91919c8 100644 --- a/zh-cn/application-dev/reference/arkui-ts/ts-state-management.md +++ b/zh-cn/application-dev/reference/arkui-ts/ts-state-management.md @@ -193,7 +193,7 @@ let value: number = AppStorage.get('PropA'); // 47 static set<T>(propName: string, newValue: T): boolean -在AppStorage中设置propName对应属性的值。 +在AppStorage中设置propName对应属性的值。如果newValue的值和propName对应属性的值相同,即不需要做赋值操作,状态变量不会通知UI刷新propName对应属性的值。 **参数:** @@ -220,7 +220,8 @@ let res1: boolean = AppStorage.set('PropB', 47) // false static setOrCreate<T>(propName: string, newValue: T): void -propName如果已经在AppStorage中存在,则设置propName对应是属性的值为newValue。如果不存在,则创建propName属性,值为newValue。 +如果propName已经在AppStorage中存在,并且newValue和propName对应属性的值不同,则设置propName对应属性的值为newValue,否则状态变量不会通知UI刷新propName对应属性的值。 +如果propName不存在,则创建propName属性,值为newValue。 **参数:** @@ -534,7 +535,7 @@ let res1: boolean = AppStorage.Set('PropB', 47) // false static SetOrCreate<T>(propName: string, newValue: T): void -propName如果已经在AppStorage中存在,则设置propName对应是属性的值为newValue。如果不存在,则创建propName属性,值为newValue。 +如果propName已经在AppStorage中存在,则设置propName对应是属性的值为newValue。如果不存在,则创建propName属性,值为newValue。 从API version 10开始废弃,推荐使用[setOrCreate10+](#setorcreate10)。 @@ -802,7 +803,7 @@ let value: number = storage.get('PropA'); // 47 set<T>(propName: string, newValue: T): boolean -在LocalStorage中设置propName对应属性的值。 +在LocalStorage中设置propName对应属性的值。如果newValue的值和propName对应属性的值相同,即不需要做赋值操作,状态变量不会通知UI刷新propName对应属性的值。 从API version 9开始,该接口支持在ArkTS卡片中使用。 @@ -831,7 +832,8 @@ let res1: boolean = storage.set('PropB', 47); // false setOrCreate<T>(propName: string, newValue: T): boolean -propName如果已经在LocalStorage中存在,则设置propName对应是属性的值为newValue。如果不存在,则创建propName属性,初始化为newValue。 +如果propName已经在AppStorage中存在,并且newValue和propName对应属性的值不同,则设置propName对应属性的值为newValue,否则状态变量不会通知UI刷新propName对应属性的值。 +如果propName不存在,则创建propName属性,值为newValue。 从API version 9开始,该接口支持在ArkTS卡片中使用。