source
stringlengths
14
113
code
stringlengths
10
21.3k
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/pages/10-Builder.ets import { AppStorageV2, PersistenceV2, Type } from '@kit.ArkUI'; import { common, Want } from '@kit.AbilityKit'; import { Setting } from './SettingPage'; import util from '@ohos.util'; @ObservedV2 class Task { // The constructor is not implemented because @Type does not support c...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/model/TaskModel.ets export default class TaskModel { taskName: string = 'Todo'; isFinish: boolean = false; }
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/model/TaskListModel.ets import { common } from '@kit.AbilityKit'; import util from '@ohos.util'; import TaskModel from'./TaskModel'; export default class TaskListModel { tasks: TaskModel[] = []; constructor(tasks: TaskModel[]) { this.tasks = tasks; } async loadTasks(context: common.UIAbi...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/viewmodel/TaskViewModel.ets import TaskModel from '../model/TaskModel'; @ObservedV2 export default class TaskViewModel { @Trace taskName: string = 'Todo'; @Trace isFinish: boolean = false; updateTask(task: TaskModel) { this.taskName = task.taskName; this.isFinish = task.isFinish; } ...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/viewmodel/TaskListViewModel.ets import { common } from '@kit.AbilityKit'; import { Type } from '@kit.ArkUI'; import TaskListModel from '../model/TaskListModel'; import TaskViewModel from'./TaskViewModel'; @ObservedV2 export default class TaskListViewModel { @Type(TaskViewModel) @Trace tasks: TaskV...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/view/TitleView.ets @ComponentV2 export default struct TitleView { @Param tasksUnfinished: number = 0; build() { Column() { Text('To-Dos') .fontSize(40) .margin(10) Text(`Unfinished: ${this.tasksUnfinished}`) .margin({ left: 10, bottom: 10 }) } } }
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/view/ListView.ets import TaskViewModel from '../viewmodel/TaskViewModel'; import TaskListViewModel from '../viewmodel/TaskListViewModel'; import { Setting } from '../pages/SettingPage'; import { ActionButton } from './BottomView'; @ComponentV2 struct TaskItem { @Param task: TaskViewModel = new TaskV...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/view/BottomView.ets import { common, Want } from '@kit.AbilityKit'; import TaskViewModel from '../viewmodel/TaskViewModel'; import TaskListViewModel from '../viewmodel/TaskListViewModel'; @Builder export function ActionButton(text: string, onClick:() => void) { Button(text, { buttonStyle: ButtonStyl...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/pages/TodoListPage.ets import TaskListViewModel from '../viewmodel/TaskListViewModel'; import { common } from '@kit.AbilityKit'; import { AppStorageV2, PersistenceV2 } from '@kit.ArkUI'; import { Setting } from '../pages/SettingPage'; import TitleView from '../view/TitleView'; import ListView from '../...
application-dev\ui\state-management\arkts-mvvm-V2.md
// src/main/ets/pages/SettingPage.ets import { AppStorageV2 } from '@kit.ArkUI'; import { common } from '@kit.AbilityKit'; @ObservedV2 export class Setting { @Trace showCompletedTask: boolean = true; } @Entry @ComponentV2 struct SettingPage { @Local setting: Setting = AppStorageV2.connect(Setting, 'Setting', () ...
application-dev\ui\state-management\arkts-new-appstoragev2.md
// Data center // Sample.ets @ObservedV2 export class Sample { @Trace p1: number = 0; p2: number = 10; }
application-dev\ui\state-management\arkts-new-appstoragev2.md
// Page1.ets import { AppStorageV2 } from '@kit.ArkUI'; import { Sample } from '../Sample'; @Entry @ComponentV2 struct Page1 { // Create a KV pair whose key is Sample in AppStorageV2 (if the key exists, the data in AppStorageV2 is returned) and associate it with prop. @Local prop: Sample = AppStorageV2.connect(Sam...
application-dev\ui\state-management\arkts-new-appstoragev2.md
// Page2.ets import { AppStorageV2 } from '@kit.ArkUI'; import { Sample } from '../Sample'; @Builder export function Page2Builder() { Page2() } @ComponentV2 struct Page2 { // Create a KV pair whose key is Sample in AppStorageV2 (if the key exists, the data in AppStorageV2 is returned) and associate it with prop. ...
application-dev\ui\state-management\arkts-new-binding.md
@Entry @ComponentV2 struct Index { @Local value: number = 0; build() { Column() { Text(`${this.value}`) Button(`change value`).onClick(() => { this.value++; }) Star({ value: this.value!! }) } } } @ComponentV2 struct Star { @Param value: number = 0; @Event $value: (va...
application-dev\ui\state-management\arkts-new-binding.md
@Entry @ComponentV2 struct BindMenuInterface { @Local isShow: boolean = false; build() { Column() { Row() { Text('click show Menu') .bindMenu(this.isShow!!, // Two-way binding. [ { value: 'Menu1', action: () => { ...
application-dev\ui\state-management\arkts-new-componentV2.md
@ComponentV2 // Decorator struct Index { // Data declared by the struct build() { // UI defined by build } }
application-dev\ui\state-management\arkts-new-Computed.md
@Computed get varName(): T { return value; }
application-dev\ui\state-management\arkts-new-Computed.md
@Computed get fullName() { // Correct format. return this.firstName + ' ' + this.lastName; } @Computed val: number = 0; // Incorrect format. An error is reported during compilation. @Computed func() { // Incorrect usage. An error is reported during compilation. }
application-dev\ui\state-management\arkts-new-Computed.md
@Computed get fullName() { this.lastName += 'a'; // Error. The properties involved in computation cannot be changed. return this.firstName + ' ' + this.lastName; }
application-dev\ui\state-management\arkts-new-Computed.md
@ComponentV2 struct Child { @Param double: number = 100; @Event $double: (val: number) => void; build() { Button('ChildChange') .onClick(() => { this.$double(200); }) } } @Entry @ComponentV2 struct Index { @Local count: number = 100; @Computed ...
application-dev\ui\state-management\arkts-new-Computed.md
@Local a : number = 1; @Computed get b() { return this.a + ' ' + this.c; // Incorrect format. A loop b -> c -> b exists. } @Computed get c() { return this.a + ' ' + this.b; // Incorrect format. A loop c -> b -> c exists. }
application-dev\ui\state-management\arkts-new-Computed.md
@Entry @ComponentV2 struct Index { @Local firstName: string = 'Li'; @Local lastName: string = 'Hua'; age: number = 20; // Computed cannot be triggered. @Computed get fullName() { console.info("---------Computed----------"); return this.firstName + ' ' + this.lastName + this.age; } build() { ...
application-dev\ui\state-management\arkts-new-Computed.md
@ObservedV2 class Name { @Trace firstName: string = 'Li'; @Trace lastName: string = 'Hua'; @Computed get fullName() { console.info('---------Computed----------'); return this.firstName + ' ' + this.lastName; } } const name: Name = new Name(); @Entry @ComponentV2 struct Index { name1: Name = name;...
application-dev\ui\state-management\arkts-new-Computed.md
@Entry @ComponentV2 struct MyView { @Local celsius: number = 20; @Computed get fahrenheit(): number { return this.celsius * 9 / 5 + 32; // C -> F } @Computed get kelvin(): number { return (this.fahrenheit - 32) * 5 / 9 + 273.15; // F -> K } @Monitor("kelvin") onKelvinMonitor(mon: IMonitor) ...
application-dev\ui\state-management\arkts-new-Computed.md
@ObservedV2 class Article { @Trace quantity: number = 0; unitPrice: number = 0; constructor(quantity: number, unitPrice: number) { this.quantity = quantity; this.unitPrice = unitPrice; } } @Entry @ComponentV2 struct Index { @Local shoppingBasket: Article[] = [new Article(1, 20), new Article(5, 2)]; ...
application-dev\ui\state-management\arkts-new-event.md
@ComponentV2 struct Index { @Event changeFactory: ()=>void = ()=>{}; // Correct usage. @Event message: string = "abcd"; // Incorrect usage. Variable of the non-function type is decorated. } @Component struct Index { @Event changeFactory: ()=>void = ()=>{}; // Incorrect usage. An error is reported du...
application-dev\ui\state-management\arkts-new-event.md
@Entry @ComponentV2 struct Index { @Local title: string = "Title One"; @Local fontColor: Color = Color.Red; build() { Column() { Child({ title: this.title, fontColor: this.fontColor, changeFactory: (type: number) => { if (type == 1) { this.title = "Title On...
application-dev\ui\state-management\arkts-new-event.md
@ComponentV2 struct Child { @Param index: number = 0; @Event changeIndex: (val: number) => void; build() { Column() { Text(`Child index: ${this.index}`) .onClick(() => { this.changeIndex(20); console.log(`after changeIndex ${this.index}`); }) } } } @Entry @Comp...
application-dev\ui\state-management\arkts-new-getTarget.md
import { UIUtils } from '@kit.ArkUI';
application-dev\ui\state-management\arkts-new-getTarget.md
import { UIUtils } from '@kit.ArkUI'; let res = UIUtils.getTarget(2); // Incorrect usage. The input parameter is of the non-object type. @Observed class Info { name: string = "Tom"; } let info: Info = new Info(); let rawInfo: Info = UIUtils.getTarget (info); // Correct usage.
application-dev\ui\state-management\arkts-new-getTarget.md
import { UIUtils } from '@kit.ArkUI'; @Observed class Info { name: string = "Tom"; } @Entry @Component struct Index { @State info: Info = new Info(); build() { Column() { Text(`info.name: ${this.info.name}`) Button(`Change the attributes of the proxy object`) ...
application-dev\ui\state-management\arkts-new-getTarget.md
@Observed class ObservedClass { name: string = "Tom"; } class NonObservedClass { name: string = "Tom"; } let observedClass: ObservedClass = new ObservedClass(); // Proxied. let nonObservedClass: NonObservedClass = new NonObservedClass(); // Not proxied.
application-dev\ui\state-management\arkts-new-getTarget.md
@Observed class ObservedClass { name: string = "Tom"; } class NonObservedClass { name: string = "Tom"; } let observedClass: ObservedClass = new ObservedClass(); // Proxied. let nonObservedClass: NonObservedClass = new NonObservedClass(); // Not proxied. @Entry @Component struct Index { @State observedObject: Obse...
application-dev\ui\state-management\arkts-new-getTarget.md
import { UIUtils } from '@kit.ArkUI'; @Observed class ObservedClass { name: string = "Tom"; } class NonObservedClass { name: string = "Tom"; } let observedClass: ObservedClass = new ObservedClass(); // Proxied. let nonObservedClass: NonObservedClass = new NonObservedClass(); // Not proxied. let globalNumberList: nu...
application-dev\ui\state-management\arkts-new-getTarget.md
@ObservedV2 class ObservedClass { @Trace name: string = "Tom"; } let globalObservedObject: ObservedClass = new ObservedClass(); // Not proxied. let globalNumberList: number[] = [1, 2, 3]; // Not proxied. let globalSampleMap: Map<number, string> = new Map([[0, "a"], [1, "b"], [3, "c"]]); // Not proxied. let globalSamp...
application-dev\ui\state-management\arkts-new-getTarget.md
import { UIUtils } from '@kit.ArkUI'; @ObservedV2 class ObservedClass { @Trace name: string = "Tom"; } let globalObservedObject: ObservedClass = new ObservedClass(); // Not proxied. let globalNumberList: number[] = [1, 2, 3]; // Not proxied. let globalSampleMap: Map<number, string> = new Map([[0, "a"], [1, "b"], [3, ...
application-dev\ui\state-management\arkts-new-getTarget.md
// Class decorated by @ObservedV2. @ObservedV2 class Info { @Trace name: string = "Tom"; @Trace age: number = 24; } let info: Info = new Info(); // info instance passed in through Node-APIs.
application-dev\ui\state-management\arkts-new-local.md
class ComponentInfo { name: string; count: number; message: string; constructor(name: string, count: number, message: string) { this.name = name; this.count = count; this.message = message; } } @Component struct Child { @State componentInfo: ComponentInfo = new ComponentInfo("Child", 1, "Hello W...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct Index { @Local count: number = 0; @Local message: string = "Hello"; @Local flag: boolean = false; build() { Column() { Text(`${this.count}`) Text(`${this.message}`) Text(`${this.flag}`) Button("change Local") .onClick(()=>{ ...
application-dev\ui\state-management\arkts-new-local.md
class RawObject { name: string; constructor(name: string) { this.name = name; } } @ObservedV2 class ObservedObject { @Trace name: string; constructor(name: string) { this.name = name; } } @Entry @ComponentV2 struct Index { @Local rawO...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct Index { @Local numArr: number[] = [1,2,3,4,5]; @Local dimensionTwo: number[][] = [[1,2,3],[4,5,6]]; build() { Column() { Text(`${this.numArr[0]}`) Text(`${this.numArr[1]}`) Text(`${this.numArr[2]}`) Text(`${this.di...
application-dev\ui\state-management\arkts-new-local.md
@ObservedV2 class Region { @Trace x: number; @Trace y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } @ObservedV2 class Info { @Trace region: Region; @Trace name: string; constructor(name: string, x: number, y: number) { this.name = name; ...
application-dev\ui\state-management\arkts-new-local.md
@ComponentV2 struct MyComponent { @Local message: string = "Hello World"; // Correct usage. build() { } } @Component struct TestComponent { @Local message: string = "Hello World"; // Incorrect usage. An error is reported during compilation. build() { } }
application-dev\ui\state-management\arkts-new-local.md
@ComponentV2 struct ChildComponent { @Local message: string = "Hello World"; build() { } } @ComponentV2 struct MyComponent { build() { ChildComponent({ message: "Hello" }) // Incorrect usage. An error is reported during compilation. } }
application-dev\ui\state-management\arkts-new-local.md
@ObservedV2 class Info { @Trace name: string; @Trace age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } @Entry @ComponentV2 struct Index { info: Info = new Info("Tom", 25); @Local localInfo: Info = new Info("Tom", 25); build() { Column() { Text(`i...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct DatePickerExample { @Local selectedDate: Date = new Date('2021-08-08'); build() { Column() { Button('set selectedDate to 2023-07-08') .margin(10) .onClick(() => { this.selectedDate = new Date('2023-07-08'); }) Button('increase the year by...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct MapSample { @Local message: Map<number, string> = new Map([[0, "a"], [1, "b"], [3, "c"]]); build() { Row() { Column() { ForEach(Array.from(this.message.entries()), (item: [number, string]) => { Text(`${item[0]}`).fontSize(30) Text(`${item[1]}`).fontS...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct SetSample { @Local message: Set<number> = new Set([0, 1, 2, 3, 4]); build() { Row() { Column() { ForEach(Array.from(this.message.entries()), (item: [number, string]) => { Text(`${item[0]}`).fontSize(30) Divider() }) Button('init set')...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct Index { @Local count: number | undefined = 10; build() { Column() { Text(`count(${this.count})`) Button("change to undefined") .onClick(() => { this.count = undefined; }) Button("change to number") .onClick(() => { this.co...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct Index { list: string[][] = [['a'], ['b'], ['c']]; @Local dataObjFromList: string[] = this.list[0]; @Monitor("dataObjFromList") onStrChange(monitor: IMonitor) { console.log("dataObjFromList has changed"); } build() { Column() { Button('change to self').onClick(() =>...
application-dev\ui\state-management\arkts-new-local.md
import { UIUtils } from '@ohos.arkui.StateManagement'; @Entry @ComponentV2 struct Index { list: string[][] = [['a'], ['b'], ['c']]; @Local dataObjFromList: string[] = this.list[0]; @Monitor("dataObjFromList") onStrChange(monitor: IMonitor) { console.log("dataObjFromList has changed"); } build() { ...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct Index { @Local w: number = 50; // Width. @Local h: number = 50; // Height. @Local message: string = 'Hello'; build() { Column() { Button('change size') .margin(20) .onClick(() => { // Values are changed additionally before the animation is executed...
application-dev\ui\state-management\arkts-new-local.md
@Entry @ComponentV2 struct Index { @Local w: number = 50; // Width. @Local h: number = 50; // Height. @Local message: string = 'Hello'; build() { Column() { Button('change size') .margin(20) .onClick(() => { // Values are changed additionally before the animation is executed...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI';
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; let res1 = UIUtils.makeObserved(2); // Invalid input parameter. An error is reported during compilation. let res2 = UIUtils.makeObserved(undefined); // Invalid input parameter. The parameter itself is returned, that is, res2 = = = undefined. let res3 = UIUtils.makeObserved(nu...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; @ObservedV2 class Info { @Trace id: number = 0; } // Incorrect usage: If makeObserved finds that the input instance is an instance of a class decorated by @ObservedV2, makeObserved returns the input object itself. let observedInfo: Info = UIUtils.makeObserved(new Info...
application-dev\ui\state-management\arkts-new-makeObserved.md
// Incorrect usage. An exception occurs during running. @State message: Info = UIUtils.makeObserved(new Info(20));
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; class Person { age: number = 10; } class Info { id: number = 0; person: Person = new Person(); } @Entry @Component struct Index { @State message: Info = new Info(); @State message2: Info = UIUtils.makeObserved(this.message); // An exception does no...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; class Info { id: number = 0; constructor(id: number) { this.id = id; } } @Entry @ComponentV2 struct Index { @Local message: Info = UIUtils.makeObserved(new Info(20)); build() { Column() { Button(`change id`).onClick(() => { ...
application-dev\ui\state-management\arkts-new-makeObserved.md
// SendableData.ets @Sendable export class SendableData { name: string = 'Tom'; age: number = 20; gender: number = 1; // Other attributes are omitted here. likes: number = 1; follow: boolean = false; }
application-dev\ui\state-management\arkts-new-makeObserved.md
import { taskpool } from '@kit.ArkTS'; import { SendableData } from './SendableData'; import { UIUtils } from '@kit.ArkUI'; @Concurrent function threadGetData(param: string): SendableData { // Process data in the subthread. let ret = new SendableData(); console.info(`Concurrent threadGetData, param ${param}`); ...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { collections } from '@kit.ArkTS'; import { UIUtils } from '@kit.ArkUI'; @Sendable class Info { id: number = 0; name: string = 'cc'; constructor(id: number) { this.id = id; } } @Entry @ComponentV2 struct Index { scroller: Scroller = new Scroller(); @Local arrCollect: collections.Array<Info> =...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { collections } from '@kit.ArkTS'; import { UIUtils } from '@kit.ArkUI'; @Sendable class Info { id: number = 0; constructor(id: number) { this.id = id; } } @Entry @ComponentV2 struct CollectionMap { mapCollect: collections.Map<string, Info> = UIUtils.makeObserved(new collections.Map<string, Info>...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { collections } from '@kit.ArkTS'; import { UIUtils } from '@kit.ArkUI'; @Sendable class Info { id: number = 0; constructor(id: number) { this.id = id; } } @Entry @ComponentV2 struct Index { set: collections.Set<Info> = UIUtils.makeObserved(new collections.Set<Info>([new Info(10), new Info(20)]));...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { JSON } from '@kit.ArkTS'; import { UIUtils } from '@kit.ArkUI'; class Info { id: number = 0; constructor(id: number) { this.id = id; } } let test: Record<string, number> = { "a": 123 }; let testJsonStr: string = JSON.stringify(test); let test2: Record<string, Info> = { "a": new Info(20) }; let tes...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; class Info { id: number = 0; age: number = 20; constructor(id: number) { this.id = id; } } @Entry @ComponentV2 struct Index { @Local message: Info = UIUtils.makeObserved(new Info(20)); @Monitor('message.id') onStrChange(monitor: IMonitor) { console.log(`na...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; class Info { id: number = 0; constructor(id: number) { this.id = id; } } @Entry @Component struct Index { // Using makeObserved together with @State, a runtime exception is thrown. message: Info = UIUtils.makeObserved(new Info(20)); build() { RelativeContain...
application-dev\ui\state-management\arkts-new-makeObserved.md
import { UIUtils } from '@kit.ArkUI'; class Info { id: number = 0; } @Entry @Component struct Index { observedObj: Info = UIUtils.makeObserved(new Info()); build() { Column() { Text(`${this.observedObj.id}`) .fontSize(50) .onClick(() => { // Use getTarget to obtain the origina...
application-dev\ui\state-management\arkts-new-monitor.md
@Observed class Info { name: string = "Tom"; age: number = 25; } @Entry @Component struct Index { @State @Watch('onInfoChange') info: Info = new Info(); @State @Watch('onNumArrChange') numArr: number[] = [1,2,3,4,5]; onInfoChange() { console.log(`info after change name: ${this.info.name}, age: ${this.inf...
application-dev\ui\state-management\arkts-new-monitor.md
@Entry @ComponentV2 struct Index { @Local message: string = "Hello World"; @Local name: string = "Tom"; @Local age: number = 24; @Monitor("message", "name") onStrChange(monitor: IMonitor) { monitor.dirty.forEach((path: string) => { console.log(`${path} changed from ${monitor.value(...
application-dev\ui\state-management\arkts-new-monitor.md
class Info { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } @Entry @ComponentV2 struct Index { @Local info: Info = new Info("Tom", 25); @Monitor("info") infoChange(monitor: IMonitor) { console.log(`info chan...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace name: string = "Tom"; @Trace region: string = "North"; @Trace job: string = "Teacher"; age: number = 25; // name is decorated by @Trace. Can listen for the change. @Monitor("name") onNameChange(monitor: IMonitor) { console.log(`name change from ${monitor.value()?.before...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Inner { @Trace num: number = 0; } @ObservedV2 class Outer { inner: Inner = new Inner(); @Monitor("inner.num") onChange(monitor: IMonitor) { console.log(`inner.num change from ${monitor.value()?.before} to ${monitor.value()?.now}`); } } @Entry @ComponentV2 struct Index { outer: Outer = ...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Base { @Trace name: string; // Listen for the name property of the base class. @Monitor("name") onBaseNameChange(monitor: IMonitor) { console.log(`Base Class name change`); } constructor(name: string) { this.name = name; } } @ObservedV2 class Derived extends Base { // Listen fo...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace name: string; @Trace age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } @ObservedV2 class ArrMonitor { @Trace dimensionTwo: number[][] = [[1,1,1],[2,2,2],[3,3,3]]; @Trace dimensionThree: number[][][] = [[[1],[2],[3]],[[4],[5...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace person: Person; @Monitor("person.name") onNameChange(monitor: IMonitor) { console.log(`name change from ${monitor.value()?.before} to ${monitor.value()?.now}`); } @Monitor("person.age") onAgeChange(monitor: IMonitor) { console.log(`age change from ${monitor.value()?.b...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Frequence { @Trace count: number = 0; @Monitor("count") onCountChange(monitor: IMonitor) { console.log(`count change from ${monitor.value()?.before} to ${monitor.value()?.now}`); } } @Entry @ComponentV2 struct Index { frequence: Frequence = new Frequence(); build() { Column() { ...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace name: string = "Tom"; @Monitor("name") onNameChange(monitor: IMonitor) { console.log(`onNameChange`); } @Monitor("name") onNameChangeDuplicate(monitor: IMonitor) { console.log(`onNameChangeDuplicate`); } } @Entry @ComponentV2 struct Index { info: Info = new Info()...
application-dev\ui\state-management\arkts-new-monitor.md
const t2: string = "t2"; // Constant enum ENUM { T3 = "t3" // Enum }; let t4: string = "t4"; // Variable @ObservedV2 class Info { @Trace t1: number = 0; @Trace t2: number = 0; @Trace t3: number = 0; @Trace t4: number = 0; @Trace t5: number = 0; @Monitor("t1") // String literal onT1Change(monitor: IMonit...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace count: number = 0; @Monitor("count") onCountChange(monitor: IMonitor) { this.count++; // Avoid using this method because it may cause infinite loops. } }
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace value: number = 50; } @ObservedV2 class UIStyle { info: Info = new Info(); @Trace color: Color = Color.Black; @Trace fontSize: number = 45; @Monitor("info.value") onValueChange(monitor: IMonitor) { let lastValue: number = monitor.value()?.before as number; let curValu...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace message: string = "not initialized"; constructor() { console.log("in constructor message change to initialized"); this.message = "initialized"; } } @ComponentV2 struct Child { @Param info: Info = new Info(); @Monitor("info.message") onMessageChange(monitor: IMonitor)...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { @Trace message: string = "not initialized"; constructor() { this.message = "initialized"; } @Monitor("message") onMessageChange(monitor: IMonitor) { console.log(`message change from ${monitor.value()?.before} to ${monitor.value()?.now}`); } } @Entry @ComponentV2 struct Ind...
application-dev\ui\state-management\arkts-new-monitor.md
message change from initialized to Index aboutToAppear message change from Index aboutToAppear to Index click to change message
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class InfoWrapper { info?: Info; constructor(info: Info) { this.info = info; } @Monitor("info.age") onInfoAgeChange(monitor: IMonitor) { console.log(`age change from ${monitor.value()?.before} to ${monitor.value()?.now}`) } } @ObservedV2 class Info { @Trace age: number; constructor(a...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class InfoWrapper { info?: Info; constructor(info: Info) { this.info = info; } } @ObservedV2 class Info { @Trace age: number; constructor(age: number) { this.age = age; } } @ComponentV2 struct Child { @Param @Require infoWrapper: InfoWrapper; @Monitor("infoWrapper.info.age") onInfo...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class InfoWrapper { info?: Info; constructor(info: Info) { this.info = info; } @Monitor("info.age") onInfoAgeChange(monitor: IMonitor) { console.log(`age change from ${monitor.value()?.before} to ${monitor.value()?.now}`) } } @ObservedV2 class Info { @Trace age: number; constructor(a...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { name: string = "John"; @Trace age: number = 24; @Monitor("age", "name") // Listen for the state variable "age" and non-state variable "name" at the same time. onPropertyChange(monitor: IMonitor) { monitor.dirty.forEach((path: string) => { console.log(`property path:${path} cha...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { name: string = "John"; @Trace age: number = 24; @Monitor("age") // Only listen for the state variable "age". onPropertyChange(monitor: IMonitor) { monitor.dirty.forEach((path: string) => { console.log(`property path:${path} change from ${monitor.value(path)?.before} to ${monit...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { name: string = "John"; @Trace age: number = 24; get myAge() { return this.age; // age is a state variable. } @Monitor("myAge") // Listen for non-@Computed decorated getter accessor. onPropertyChange() { console.log("age changed"); } } @Entry @ComponentV2 struct Index { i...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { name: string = "John"; @Trace age: number = 24; @Computed // Add @Computed to myAge as a state variable. get myAge() { return this.age; } @Monitor("myAge") // Listen for @Computed decorated getter accessor. onPropertyChange() { console.log("age changed"); } } @Entry @Com...
application-dev\ui\state-management\arkts-new-monitor.md
@ObservedV2 class Info { name: string = "John"; @Trace age: number = 24; @Monitor("age") // Only listen for the state variable "age". onPropertyChange() { console.log("age changed"); } } @Entry @ComponentV2 struct Index { info: Info = new Info(); build() { Column() { Button("change age") ...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@Observed class Father { son: Son; constructor(name: string, age: number) { this.son = new Son(name, age); } } @Observed class Son { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } @Entry @Component struct Index { @State father: Fat...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@Observed class Father { son: Son; constructor(name: string, age: number) { this.son = new Son(name, age); } } @Observed class Son { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } @Component struct Child { @ObjectLink son: Son; ...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Son { @Trace age: number = 100; } class Father { son: Son = new Son(); } @Entry @ComponentV2 struct Index { father: Father = new Father(); build() { Column() { // If age is changed, the Text component is re-rendered. Text(`${this.father.son.age}`) .onClick(() => { ...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Father { @Trace name: string = "Tom"; } class Son extends Father { } @Entry @ComponentV2 struct Index { son: Son = new Son(); build() { Column() { // If name is changed, the Text component is re-rendered. Text(`${this.son.name}`) .onClick(() => { this.son.name ...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Manager { @Trace static count: number = 1; } @Entry @ComponentV2 struct Index { build() { Column() { // If count is changed, the Text component is re-rendered. Text(`${Manager.count}`) .onClick(() => { Manager.count++; }) } } }
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Person { id: number = 0; @Trace age: number = 8; } @Entry @ComponentV2 struct Index { person: Person = new Person(); build() { Column() { // age is decorated by @Trace and can trigger re-renders when used in the UI. Text(`${this.person.age}`) .onClick(() => { ...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 // Incorrect usage. An error is reported during compilation. struct Index { build() { } }
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
class User { id: number = 0; @Trace name: string = "Tom"; // Incorrect usage. An error is reported at compile time. }