source
stringlengths
14
113
code
stringlengths
10
21.3k
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ComponentV2 struct Comp { @Trace message: string = "Hello World"; // Incorrect usage. An error is reported at compile time. build() { } }
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@Observed class User { @Trace name: string = "Tom"; // Incorrect usage. An error is reported at compile time. } @ObservedV2 class Person { @Track name: string = "Jack"; // Incorrect usage. An error is reported at compile time. }
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
// @State is used as an example. @ObservedV2 class Job { @Trace jobName: string = "Teacher"; } @ObservedV2 class Info { @Trace name: string = "Tom"; @Trace age: number = 25; job: Job = new Job(); } @Entry @Component struct Index { @State info: Info = new Info(); // As @State is not allowed here, an error is r...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
// @State is used as an example. @ObservedV2 class Job { @Trace jobName: string = "Teacher"; } @ObservedV2 class Info { @Trace name: string = "Tom"; @Trace age: number = 25; job: Job = new Job(); } class Message extends Info { constructor() { super(); } } @Entry @Component struct Index { @Stat...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Pencil { @Trace length: number = 21; // If length changes, the bound component is re-rendered. } class Bag { width: number = 50; height: number = 60; pencil: Pencil = new Pencil(); } class Son { age: number = 5; school: string = "some"; bag: Bag = new Bag(); } @Entry @ComponentV2 struct...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class GrandFather { @Trace age: number = 0; constructor(age: number) { this.age = age; } } class Father extends GrandFather{ constructor(father: number) { super(father); } } class Uncle extends GrandFather { constructor(uncle: number) { super(uncle); } } class Son extends Father {...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
let nextId: number = 0; @ObservedV2 class Arr { id: number = 0; @Trace numberArr: number[] = []; constructor() { this.id = nextId++; this.numberArr = [0, 1, 2]; } } @Entry @ComponentV2 struct Index { arr: Arr = new Arr(); build() { Column() { Text(`length: ${this.arr.numberArr.length}`...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
let nextId: number = 0; @ObservedV2 class Person { @Trace age: number = 0; constructor(age: number) { this.age = age; } } @ObservedV2 class Info { id: number = 0; @Trace personList: Person[] = []; constructor() { this.id = nextId++; this.personList = [new Person(0), new Person(1), new Person...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Info { @Trace memberMap: Map<number, string> = new Map([[0, "a"], [1, "b"], [3, "c"]]); } @Entry @ComponentV2 struct MapSample { info: Info = new Info(); build() { Row() { Column() { ForEach(Array.from(this.info.memberMap.entries()), (item: [number, string]) => { Te...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Info { @Trace memberSet: Set<number> = new Set([0, 1, 2, 3, 4]); } @Entry @ComponentV2 struct SetSample { info: Info = new Info(); build() { Row() { Column() { ForEach(Array.from(this.info.memberSet.entries()), (item: [number, string]) => { Text(`${item[0]}`) ...
application-dev\ui\state-management\arkts-new-observedV2-and-trace.md
@ObservedV2 class Info { @Trace selectedDate: Date = new Date('2021-08-08') } @Entry @ComponentV2 struct DateSample { info: Info = new Info() build() { Column() { Button('set selectedDate to 2023-07-08') .margin(10) .onClick(() => { this.info.selectedDate = new Date('2023-07-...
application-dev\ui\state-management\arkts-new-once.md
@ComponentV2 struct MyComponent { @Param @Once onceParam: string = "onceParam"; // Correct usage. @Once onceStr: string = "Once"; // Incorrect usage. @Once cannot be used independently. @Local @Once onceLocal: string = "onceLocal"; // Incorrect usage. @Once cannot be used with @Local. } @Component s...
application-dev\ui\state-management\arkts-new-once.md
@ComponentV2 struct MyComponent { @Param @Once param1: number; @Once @Param param2: number; }
application-dev\ui\state-management\arkts-new-once.md
@ComponentV2 struct ChildComponent { @Param @Once onceParam: string = ""; build() { Column() { Text(`onceParam: ${this.onceParam}`) } } } @Entry @ComponentV2 struct MyComponent { @Local message: string = "Hello World"; build() { Column() { Text(`Parent message: ${this.message}`) Butt...
application-dev\ui\state-management\arkts-new-once.md
@ObservedV2 class Info { @Trace name: string; constructor(name: string) { this.name = name; } } @ComponentV2 struct Child { @Param @Once onceParamNum: number = 0; @Param @Once @Require onceParamInfo: Info; build() { Column() { Text(`Child onceParamNum: ${this.onceParamNum}`) Text(`Child...
application-dev\ui\state-management\arkts-new-param.md
@Observed class Region { x: number; y: number; constructor(x: number, y: number) { this.x = x; this.y = y; } } @Observed class Info { region: Region; constructor(x: number, y: number) { this.region = new Region(x, y); } } @Entry @Component struct Index { @State info: Info = new Info(0, 0); ...
application-dev\ui\state-management\arkts-new-param.md
@Entry @ComponentV2 struct Index { @Local count: number = 0; @Local message: string = "Hello"; @Local flag: boolean = false; build() { Column() { Text(`Local ${this.count}`) Text(`Local ${this.message}`) Text(`Local ${this.flag}`) Button("change Local") ...
application-dev\ui\state-management\arkts-new-param.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 rawObject: RawObject = new RawObject...
application-dev\ui\state-management\arkts-new-param.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.dimensionTwo[0][0]}`) ...
application-dev\ui\state-management\arkts-new-param.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-param.md
@ComponentV2 struct MyComponent { @Param message: string = "Hello World"; // Correct usage. build() { } } @Component struct TestComponent { @Param message: string = "Hello World"; // Incorrect usage. An error is reported during compilation. build() { } }
application-dev\ui\state-management\arkts-new-param.md
@ComponentV2 struct ChildComponent { @Param param1: string = "Initialize local"; @Param param2: string = "Initialize local and put in"; @Require @Param param3: string; @Param param4: string; // Incorrect usage. The external initialization is not performed and no initial value exists in the local host....
application-dev\ui\state-management\arkts-new-param.md
@ObservedV2 class Info { @Trace name: string; constructor(name: string) { this.name = name; } } @Entry @ComponentV2 struct Index { @Local info: Info = new Info("Tom"); build() { Column() { Text(`Parent info.name ${this.info.name}`) Button("Parent change info") ...
application-dev\ui\state-management\arkts-new-param.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 name: string; @Trace age: number; @Trace region: Region; constructor(name: string, age: number, x: number, y: number) { this.name = n...
application-dev\ui\state-management\arkts-new-param.md
@ComponentV2 struct DateComponent { @Param selectedDate: Date = new Date('2024-01-01'); build() { Column() { DatePicker({ start: new Date('1970-1-1'), end: new Date('2100-1-1'), selected: this.selectedDate }) } } } @Entry @ComponentV2 struct ParentComponent { @Local...
application-dev\ui\state-management\arkts-new-param.md
@ComponentV2 struct Child { @Param value: Map<number, string> = new Map() build() { Column() { ForEach(Array.from(this.value.entries()), (item: [number, string]) => { Text(`${item[0]}`).fontSize(30) Text(`${item[1]}`).fontSize(30) Divider() }) } } } @Entry @ComponentV2...
application-dev\ui\state-management\arkts-new-param.md
@ComponentV2 struct Child { @Param message: Set<number> = new Set() build() { Column() { ForEach(Array.from(this.message.entries()), (item: [number, string]) => { Text(`${item[0]}`).fontSize(30) Divider() }) } .width('100%') } } @Entry @ComponentV2 struct SetSample11 { @...
application-dev\ui\state-management\arkts-new-param.md
@Entry @ComponentV2 struct Index { @Local count: number | undefined = 0; build() { Column() { MyComponent({ count: this.count }) Button('change') .onClick(() => { this.count = undefined; }) } } } @ComponentV2 struct MyComponent { @Param count: number | undefined =...
application-dev\ui\state-management\arkts-new-persistencev2.md
// globalConnect API static globalConnect<T extends object>( type: ConnectOptions<T> ): T | undefined;
application-dev\ui\state-management\arkts-new-persistencev2.md
// ConnectOptions parameters class ConnectOptions<T extends object> { type: TypeConstructorWithArgs<T>; // (Mandatory) Specified type. key?: string; // (Optional) Input key. If no key is specified, the name of the type is used as the key. defaultCreator?: StorageDefaultCreator<T> // Default constructor. You are a...
application-dev\ui\state-management\arkts-new-persistencev2.md
static remove<T>(keyOrType: string | TypeConstructorWithArgs<T>): void;
application-dev\ui\state-management\arkts-new-persistencev2.md
static keys(): Array<string>;
application-dev\ui\state-management\arkts-new-persistencev2.md
static save<T>(keyOrType: string | TypeConstructorWithArgs<T>): void;
application-dev\ui\state-management\arkts-new-persistencev2.md
static notifyOnError(callback: PersistenceErrorCallback | undefined): void;
application-dev\ui\state-management\arkts-new-persistencev2.md
// Sample.ets import { Type } from '@kit.ArkUI'; // Data center @ObservedV2 class SampleChild { @Trace p1: number = 0; p2: number = 10; } @ObservedV2 export class Sample { // Complex objects need to be decorated by @Type to ensure successful serialization. @Type(SampleChild) @Trace f: SampleChild = new Samp...
application-dev\ui\state-management\arkts-new-persistencev2.md
// Page1.ets import { PersistenceV2 } from '@kit.ArkUI'; import { Sample } from '../Sample'; // Callback used to receive serialization failure. PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => { console.error(`error key: ${key}, reason: ${reason}, message: ${msg}`); }); @Entry @ComponentV2 ...
application-dev\ui\state-management\arkts-new-persistencev2.md
// Page2.ets import { PersistenceV2 } from '@kit.ArkUI'; import { Sample } from '../Sample'; @Builder export function Page2Builder() { Page2() } @ComponentV2 struct Page2 { // Create a KV pair whose key is Sample in PersistenceV2 (if the key exists, the data in PersistenceV2 is returned) and associate it with pro...
application-dev\ui\state-management\arkts-new-persistencev2.md
import { PersistenceV2, Type, ConnectOptions } from '@kit.ArkUI'; import { contextConstant } from '@kit.AbilityKit'; // Callback used to receive serialization failure. PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => { console.error(`error key: ${key}, reason: ${reason}, message: ${msg}`); }...
application-dev\ui\state-management\arkts-new-persistencev2.md
// Module 1 import { PersistenceV2, Type } from '@kit.ArkUI'; import { contextConstant, common, Want } from '@kit.AbilityKit'; // Callback used to receive serialization failure. PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => { console.error(`error key: ${key}, reason: ${reason}, message: $...
application-dev\ui\state-management\arkts-new-persistencev2.md
// Module 2 import { PersistenceV2, Type } from '@kit.ArkUI'; import { contextConstant } from '@kit.AbilityKit'; // Callback used to receive serialization failure. PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => { console.error(`error key: ${key}, reason: ${reason}, message: ${msg}`); }); ...
application-dev\ui\state-management\arkts-new-persistencev2.md
// Use connect to store data. import { PersistenceV2, Type } from '@kit.ArkUI'; // Callback used to receive serialization failure. PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => { console.error(`error key: ${key}, reason: ${reason}, message: ${msg}`); }); @ObservedV2 class SampleChild { ...
application-dev\ui\state-management\arkts-new-persistencev2.md
// Migrate to GlobalConnect. import { PersistenceV2, Type } from '@kit.ArkUI'; // Callback used to receive serialization failure. PersistenceV2.notifyOnError((key: string, reason: string, msg: string) => { console.error(`error key: ${key}, reason: ${reason}, message: ${msg}`); }); @ObservedV2 class SampleChild { ...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@ComponentV2 struct Parent { // The aliasName is not defined. Use the property name "str" as the aliasName. @Provider() str: string = 'hello'; } @ComponentV2 struct Child { // Define aliasName as"str" and use it to search. // If the value can be found on the Parent component, use the value "hello" of @Provider...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@ComponentV2 struct Parent { // Define aliasName as "alias". @Provider('alias') str: string = 'hello'; } @ComponentV2 struct Child { // Define aliasName as "alias", find out @Provider, and obtain the value "hello". @Consumer('alias') str: string = 'world'; }
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@ComponentV2 struct Parent { // Define aliasName as "alias". @Provider('alias') str: string = 'hello'; } @ComponentV2 struct Child { // The aliasName is not defined. Use the property name "str" as the aliasName. // The corresponding @Provider is not found, use the local value "world". @Consumer() str: string...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@Entry @ComponentV2 struct Parent { @Provider() str: string = 'hello'; build() { Column() { Button(this.str) .onClick(() => { this.str += '0'; }) Child() } } } @ComponentV2 struct Child { @Consumer() str: string = 'world'; build() { Column() { Button(...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@Entry @ComponentV2 struct Parent { @Provider() str1: string = 'hello'; build() { Column() { Button(this.str1) .onClick(() => { this.str1 += '0'; }) Child() } } } @ComponentV2 struct Child { @Consumer() str: string = 'world'; build() { Column() { Butt...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@Entry @ComponentV2 struct Parent { @Local childX: number = 0; @Local childY: number = 1; @Provider() onDrag: (x: number, y: number) => void = (x: number, y: number) => { console.log(`onDrag event at x=${x} y:${y}`); this.childX = x; this.childY = y; } build() { Column() { Text(`child p...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@ObservedV2 class User { @Trace name: string; @Trace age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } const data: User[] = [new User('Json', 10), new User('Eric', 15)]; @Entry @ComponentV2 struct Parent { @Provider('data') users: User[] = data; build() { ...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@Entry @ComponentV2 struct Index { @Provider() val: number = 10; build() { Column() { Parent() } } } @ComponentV2 struct Parent { @Provider() val: number = 20; @Consumer("val") val2: number = 0; // 10 build() { Column() { Text(`${this.val2}`) Child() } } } @ComponentV...
application-dev\ui\state-management\arkts-new-Provider-and-Consumer.md
@Entry @ComponentV2 struct Index { @Provider() val: number = 10; build() { Column() { Parent({ val2: this.val }) } } } @ComponentV2 struct Parent { @Consumer() val: number = 0; @Param val2: number = 0; build() { Column() { Text(`Parent @Consumer val: ${this.val}`).fontSize(30).onC...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
// Use the virtualScroll mode in the List container component. @Entry @ComponentV2 // The decorator of V2 is recommended. struct RepeatExample { @Local dataArr: Array<string> = []; // Data source aboutToAppear(): void { for (let i = 0; i < 50; i++) { this.dataArr.push(`data_${i}`); // Add data to the arr...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct Parent { @Local simpleList: Array<string> = ['one', 'two', 'three']; build() { Row() { Column() { Text('Click to change the value of the third array item') .fontSize(24) .fontColor(Color.Red) .onClick(() => { this.simpleList[2] ...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct Parent { @Local simpleList: Array<string> = ['one', 'two', 'three']; build() { Row() { Column() { Text('Exchange array items 1 and 2') .fontSize(24) .fontColor(Color.Red) .onClick(() => { let temp: string = this.simpleList[2] ...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@ObservedV2 class Repeat005Clazz { @Trace message: string = ''; constructor(message: string) { this.message = message; } } @Entry @ComponentV2 struct RepeatVirtualScroll { @Local simpleList: Array<Repeat005Clazz> = []; private exchange: number[] = []; private counter: number = 0; @Local selectOption...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@ObservedV2 class Repeat006Clazz { @Trace message: string = ''; constructor(message: string) { this.message = message; } } @Entry @ComponentV2 struct RepeatVirtualScroll2T { @Local simpleList: Array<Repeat006Clazz> = []; private exchange: number[] = []; private counter: number = 0; @Local selectOpti...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct RepeatLazyLoading { // Assume that the total length of the data source is 1000. The initial array does not provide data. @Local arr: Array<string> = []; scroller: Scroller = new Scroller(); build() { Column({ space: 5 }) { // The initial index displayed on the screen is 100....
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct RepeatLazyLoading { @Local arr: Array<string> = []; build() { Column({ space: 5 }) { List({ space: 5 }) { Repeat(this.arr) .virtualScroll({ onTotalCount: () => { return 100; }, // Implement lazy loading. onLazyLoading: (index...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct RepeatLazyLoading { @Local arr: Array<string> = []; // Provide the initial data required for the first screen display. aboutToAppear(): void { for (let i = 0; i < 15; i++) { this.arr.push(i.toString()); } } build() { Column({ space: 5 }) { List({ space: 5 }) ...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
// Repeat can be nested in other components. @Entry @ComponentV2 struct RepeatNest { @Local outerList: string[] = []; @Local innerList: number[] = []; aboutToAppear(): void { for (let i = 0; i < 20; i++) { this.outerList.push(i.toString()); this.innerList.push(i); } } build() { Colum...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
class DemoListItemInfo { name: string; icon: Resource; constructor(name: string, icon: Resource) { this.name = name; this.icon = icon; } } @Entry @ComponentV2 struct DemoList { @Local videoList: Array<DemoListItemInfo> = []; aboutToAppear(): void { for (let i = 0; i < 10; i++) { // app....
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
class DemoGridItemInfo { name: string; icon: Resource; constructor(name: string, icon: Resource) { this.name = name; this.icon = icon; } } @Entry @ComponentV2 struct DemoGrid { @Local itemList: Array<DemoGridItemInfo> = []; @Local isRefreshing: boolean = false; private layoutOptions: GridLayoutO...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
const remotePictures: Array<string> = [ 'https://www.example.com/xxx/0001.jpg', // Set the specific network image address. 'https://www.example.com/xxx/0002.jpg', 'https://www.example.com/xxx/0003.jpg', 'https://www.example.com/xxx/0004.jpg', 'https://www.example.com/xxx/0005.jpg', 'https://www.example.com/...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct RepeatVirtualScrollOnMove { @Local simpleList: Array<string> = []; aboutToAppear(): void { for (let i = 0; i < 100; i++) { this.simpleList.push(`${i}`); } } build() { Column() { List() { Repeat<string>(this.simpleList) // Set onMove to enabl...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@ObservedV2 class RepeatData { @Trace id: string; @Trace msg: string; constructor(id: string, msg: string) { this.id = id; this.msg = msg; } } @Entry @ComponentV2 struct RepeatRerender { @Local dataArr: Array<RepeatData> = []; aboutToAppear(): void { for (let i = 0; i < 10; i++) { this....
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@ObservedV2 class RepeatData { @Trace id: string; @Trace msg: string; constructor(id: string, msg: string) { this.id = id; this.msg = msg; } } @Entry @ComponentV2 struct RepeatRerender { @Local dataArr: Array<RepeatData> = []; aboutToAppear(): void { for (let i = 0; i < 10; i++) { this....
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
// Define a class and mark it as observable. // Customize an array in the class and mark it as traceable. @ObservedV2 class ArrayHolder { @Trace arr: Array<number> = []; // constructor, used to initialize arrays. constructor(count: number) { for (let i = 0; i < count; i++) { this.arr.push(i); } }...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
// The definition of ArrayHolder is the same as that in the demo code. @Entry @ComponentV2 struct RepeatTemplateSingle { @Local arrayHolder: ArrayHolder = new ArrayHolder(100); @Local totalCount: number = this.arrayHolder.arr.length; scroller: Scroller = new Scroller(); private start: number = 1; private en...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@ObservedV2 class VehicleData { @Trace name: string; @Trace price: number; constructor(name: string, price: number) { this.name = name; this.price = price; } } @ObservedV2 class VehicleDB { public vehicleItems: VehicleData[] = []; constructor() { // The initial size of the array is 20. fo...
application-dev\ui\state-management\arkts-new-rendering-control-repeat.md
@Entry @ComponentV2 struct RepeatBuilderPage { @Local simpleList1: Array<number> = []; @Local simpleList2: Array<number> = []; aboutToAppear(): void { for (let i = 0; i < 100; i++) { this.simpleList1.push(i) this.simpleList2.push(i) } } build() { Column({ space: 20 }) { Text('U...
application-dev\ui\state-management\arkts-new-reusableV2.md
@ReusableV2 // Decorates @ComponentV2 decorated custom components. @ComponentV2 struct ReusableV2Component { @Local message: string = 'Hello World'; build () { Column() { Text(this.message) } } }
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { build() { Column() { ReusableV2Component() .reuse({reuseId: () => 'reuseComponent'}) // Use 'reuseComponent' as reuseId. ReusableV2Component() .reuse({reuseId: () => ''}) // If an empty string is used, the component name 'ReusableV2Component' is used ...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { build() { Column() { ReusableV2Component() // Correct usage. V1Component() } } } @ReusableV2 @ComponentV2 struct ReusableV2Component { build() { } } @Builder function V2ReusableBuilder() { ReusableV2Component() } @C...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { @Local arr: number[] = [1, 2, 3, 4, 5]; build() { Column() { List() { Repeat(this.arr) .each(() => {}) .virtualScroll() .templateId(() => 'a') .template('a', (ri) => { ListItem() { ...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { @Local condition1: boolean = false; @Local condition2: boolean = true; build() { Column() { Button('step1. appear') .onClick(() => { this.condition1 = true; }) Button('step2. recycle') .onClick(() => { this.condition2...
application-dev\ui\state-management\arkts-new-reusableV2.md
@ObservedV2 class Info { @Trace age: number = 25; } const info: Info = new Info(); @Entry @ComponentV2 struct Index { @Local condition: boolean = true; build() { Column() { Button('Reuse/Recycle').onClick(()=>{this.condition=!this.condition;}) Button('Change value').onClick(()=>{info.age++;}) ...
application-dev\ui\state-management\arkts-new-reusableV2.md
@ObservedV2 class Info { @Trace age: number; constructor(age: number) { this.age = age; } } @Entry @ComponentV2 struct Index { @Local local: number = 0; @Provider('inherit') inheritProvider: number = 100; @Local condition: boolean = true; build() { Column() { Button('Recycle/Reuse').onClick(...
application-dev\ui\state-management\arkts-new-reusableV2.md
@ObservedV2 class Info { @Trace age: number; constructor(age: number) { this.age = age; } } @Entry @ComponentV2 struct Index { @Local condition: boolean = true; build() { Column() { Button('Recycle/Reuse').onClick(()=>{this.condition=!this.condition;}) if (this.condition) { Reusabl...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { @Local condition: boolean = true; build() { Column() { Button('Recycle/Reuse').onClick(()=>{this.condition=!this.condition;}) // Click to switch the recycle/reuse state. if (this.condition) { ReusableV2Component() } } } } @ReusableV2 @ComponentV...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { @Local simpleList: number[] = [1, 2, 3, 4, 5]; @Local condition: boolean = true; build() { Column() { Button('Delete/Create Repeat').onClick(()=>{this.condition=!this.condition;}) Button('Add element').onClick(()=>{this.simpleList.push(this.simpleList.length+1);}...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { @Local condition: boolean = true; @Local simpleList: number[] = []; aboutToAppear(): void { for (let i = 0; i < 100; i++) { this.simpleList.push(i) } } build() { Column() { Button('Change condition').onClick(()=>{this.condition=!this.condition;}) ...
application-dev\ui\state-management\arkts-new-reusableV2.md
@Entry @ComponentV2 struct Index { @Local simpleList: number[] = [0, 1, 2, 3, 4, 5]; build() { Column() { ForEach(this.simpleList, (num: number, index) => { Row() { Button('Click to change').onClick (()=>{this.simpleList[index]++;}) ReusableV2Component({ num: num }) } ...
application-dev\ui\state-management\arkts-new-reusableV2.md
class BasicDataSource implements IDataSource { private listeners: DataChangeListener[] = []; private originDataArray: StringData[] = []; public totalCount(): number { return 0; } public getData(index: number): StringData { return this.originDataArray[index]; } registerDataChangeListener(listene...
application-dev\ui\state-management\arkts-new-type.md
class Sample { data: number = 0; } @ObservedV2 class Info { @Type(Sample) @Trace sample: Sample = new Sample(); // Correct usage. } @Observed class Info2 { @Type(Sample) sample: Sample = new Sample(); // Incorrect usage. @Type cannot be used in the @Observed decorated class. Otherwise, an error is reported du...
application-dev\ui\state-management\arkts-new-type.md
import { Type } from '@kit.ArkUI'; // Data center @ObservedV2 class SampleChild { @Trace p1: number = 0; p2: number = 10; } @ObservedV2 export class Sample { // Complex objects need to be decorated by @Type to ensure successful serialization. @Type(SampleChild) @Trace f: SampleChild = new SampleChild(); }
application-dev\ui\state-management\arkts-new-type.md
import { PersistenceV2 } from '@kit.ArkUI'; import { Sample } from '../Sample'; @Entry @ComponentV2 struct Page { prop: Sample = PersistenceV2.connect(Sample, () => new Sample())!; build() { Column() { Text(`Page1 add 1 to prop.p1: ${this.prop.f.p1}`) .fontSize(30) .onClick(() => { ...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
// Allowed: assigning a value to a property of an @ObjectLink decorated variable this.objLink.a= ... // Not allowed: assigning a new value to the @ObjectLink decorated variable itself this.objLink= ...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
class Child { public num: number; constructor(num: number) { this.num = num; } } @Observed class Parent { public child: Child; public count: number; constructor(child: Child, count: number) { this.child = child; this.count = count; } }
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@ObjectLink parent: Parent; // Value changes can be observed. this.parent.child = new Child(5); this.parent.count = 5; // Child is not decorated by @Observed, therefore, its property changes cannot be observed. this.parent.child.num = 5;
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class DateClass extends Date { constructor(args: number | string) { super(args); } } @Observed class NewDate { public data: DateClass; constructor(data: DateClass) { this.data = data; } } @Component struct Child { label: string = 'date'; @ObjectLink data: DateClass; build() { C...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class Info { count: number; constructor(count: number) { this.count = count; } } class Test { msg: number; constructor(msg: number) { this.msg = msg; } } // Incorrect format. The count type is not specified, leading to a compilation error. @ObjectLink count;...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class Info { count: number; constructor(count: number) { this.count = count; } } // Incorrect format. An error is reported during compilation. @ObjectLink count: Info = new Info(10); // Correct format. @ObjectLink count: Info;
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class Info { count: number; constructor(count: number) { this.count = count; } } @Component struct Child { @ObjectLink num: Info; build() { Column() { Text(`Value of num: ${this.num.count}`) .onClick(() => { // Incorrect format. The vari...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class Info { count: number; constructor(count: number) { this.count = count; } } @Component struct Child { @ObjectLink num: Info; build() { Column() { Text(`Value of num: ${this.num.count}`) .onClick(() => { // Correct format, which is u...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class Animal { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } @Observed class Dog extends Animal { kinds: string; constructor(name: string, age: number, kinds: string) { super(name, age); this.kinds = kinds; } } @Entr...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class Book { name: string; constructor(name: string) { this.name = name; } } @Observed class Bag { book: Book; constructor(book: Book) { this.book = book; } } @Component struct BookCard { @ObjectLink book: Book; build() { Column() { Text(`BookCard: ${this.book.name}`) //...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
let NextID: number = 1; @Observed class Info { public id: number; public info: number; constructor(info: number) { this.id = NextID++; this.info = info; } } @Component struct Child { // The type of the Child's @ObjectLink is Info. @ObjectLink info: Info; label: string = 'ViewChild'; build() ...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class ObservedArray<T> extends Array<T> { }
application-dev\ui\state-management\arkts-observed-and-objectlink.md
@Observed class ObservedArray<T> extends Array<T> { } @Component struct Item { @ObjectLink itemArr: ObservedArray<string>; build() { Row() { ForEach(this.itemArr, (item: string, index: number) => { Text(`${index}: ${item}`) .width(100) .height(100) }, (item: string) => ...
application-dev\ui\state-management\arkts-observed-and-objectlink.md
import { UIUtils } from '@kit.ArkUI'; @Component struct Item { @ObjectLink itemArr: Array<string>; build() { Row() { ForEach(this.itemArr, (item: string, index: number) => { Text(`${index}: ${item}`) .width(100) .height(100) }, (item: string) => item) } } } @Entr...