source stringlengths 14 113 | code stringlengths 10 21.3k |
|---|---|
application-dev\ui\state-management\arkts-track.md | class Person {
// id is decorated by @Track.
@Track id: number;
// age is not decorated by @Track.
age: number;
constructor(id: number, age: number) {
this.id = id;
this.age = age;
}
}
@Entry
@Component
struct Parent {
@State parent: Person = new Person(2, 30);
build() {
// Property that ... |
application-dev\ui\state-management\arkts-two-way-sync.md | // xxx.ets
@Entry
@Component
struct TextInputExample {
@State text: string = ''
controller: TextInputController = new TextInputController()
build() {
Column({ space: 20 }) {
Text(this.text)
TextInput({ text: $$this.text, placeholder: 'input your word...', controller: this.controller })
.p... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct Child {
@State val: number = 10;
build(){
Text(this.val.toString())
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@ComponentV2
struct Child {
@Local val: number = 10;
build(){
Text(this.val.toString())
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | class Child {
value: number = 10;
}
@Component
@Entry
struct example {
@State child: Child = new Child();
build(){
Column() {
Text(this.child.value.toString())
// @State can be used to observe the top-level changes.
Button('value+1')
.onClick(() => {
this.child.value++;
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ObservedV2
class Child {
@Trace public value: number = 10;
}
@ComponentV2
@Entry
struct example {
@Local child: Child = new Child();
build(){
Column() {
Text(this.child.value.toString())
// @Local can only observe itself. Add @ObservedV2 and @Trace to Child.
Button('value+1')
.onCl... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
@State value: number = 0;
build() {
Text(this.value.toString())
}
}
@Entry
@Component
struct Parent {
build() {
Column(){
// @State supports external initialization.
Child({ value: 30 })
}
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
@Param @Once value: number = 0;
build() {
Text(this.value.toString())
}
}
@Entry
@ComponentV2
struct Parent {
build() {
Column(){
// @Local does not support external initialization. Use @Param and @Once instead.
Child({ value: 30 })
}
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
// @Link can synchronize data in a two-way manner.
@Link val: number;
build() {
Column(){
Text("child: " + this.val.toString())
Button("+1")
.onClick(() => {
this.val++;
})
}
}
}
@Entry
@Component
struct Parent {
@State myVal: number = 1... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
// @Param works with @Event to synchronize data in a two-way manner.
@Param val: number = 0;
@Event addOne: () => void;
build() {
Column(){
Text("child: " + this.val.toString())
Button("+1")
.onClick(()=> {
this.addOne();
})
}
}
}
@E... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
@Prop value: number;
build() {
Text(this.value.toString())
}
}
@Entry
@Component
struct Parent {
build() {
Column(){
Child({ value: 30 })
}
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
@Param value: number = 0;
build() {
Text(this.value.toString())
}
}
@Entry
@ComponentV2
struct Parent {
build() {
Column(){
Child({ value: 30 })
}
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | class Fruit {
apple: number = 5;
orange: number = 10;
}
@Component
struct Child {
// @Prop passes the Fruit class. When the properties of the child class are changed, the parent class is not affected.
@Prop fruit: Fruit;
build() {
Column() {
Text("child apple: "+ this.fruit.apple.toString())
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ObservedV2
class Fruit{
@Trace apple: number = 5;
@Trace orange: number = 10;
// Implement the deep copy to prevent the child component from changing the parent component data.
clone(): Fruit {
let newFruit: Fruit = new Fruit();
newFruit.apple = this.apple;
newFruit.orange = this.orange;
return... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
// @Prop can be used to directly change the variable.
@Prop value: number;
build() {
Column(){
Text(this.value.toString())
Button("+1")
.onClick(()=> {
this.value++;
})
}
}
}
@Entry
@Component
struct Parent {
build() {
Column(){
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
// @Param used together with @Once can change the variable locally.
@Param @Once value: number = 0;
build() {
Column(){
Text(this.value.toString())
Button("+1")
.onClick(() => {
this.value++;
})
}
}
}
@Entry
@ComponentV2
struct Parent {
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
@Prop localValue: number = 0;
build() {
Column() {
Text(`${this.localValue}`).fontSize(25)
Button('Child +100')
.onClick(() => {
// The change of localValue is not synchronized to Parent.
this.localValue += 100;
})
}
}
}
@Entry
@C... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
@Local localValue: number = 0;
@Param value: number = 0;
@Monitor('value')
onValueChange(mon: IMonitor) {
console.info(`value has been changed from ${mon.value()?.before} to ${mon.value()?.now}`);
// When the value of the Parent changes, Child is notified of the value update ... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Observed
class Address {
city: string;
constructor(city: string) {
this.city = city;
}
}
@Observed
class User {
name: string;
address: Address;
constructor(name: string, address: Address) {
this.name = name;
this.address = address;
}
}
@Component
struct AddressView {
// The address deco... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ObservedV2
class Address {
@Trace city: string;
constructor(city: string) {
this.city = city;
}
}
@ObservedV2
class User {
@Trace name: string;
@Trace address: Address;
constructor(name: string, address: Address) {
this.name = name;
this.address = address;
}
}
@Entry
@ComponentV2
struct U... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Observed
class User {
@Track name: string;
@Track age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
@Entry
@Component
struct UserProfile {
@State user: User = new User('Alice', 30);
build() {
Column() {
Text(`Name: ${this.user.name}`)
Tex... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ObservedV2
class User {
@Trace name: string;
@Trace age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
@Entry
@ComponentV2
struct UserProfile {
@Local user: User = new User('Alice', 30);
build() {
Column() {
Text(`Name: ${this.user.name}`)
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
// Both the alias and attribute name are keys and can be used to match.
@Consume('text') childMessage: string;
@Consume message: string;
build(){
Column(){
Text(this.childMessage)
Text(this.message) // The value of Text is "Hello World".
}
}
}
@Entry
@Component
s... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
// The alias is the unique matching key. If the alias exists, the attribute name cannot be used for matching.
@Consumer('text') childMessage: string = "default";
@Consumer() message: string = "default";
build(){
Column(){
Text(this.childMessage)
Text(this.message) // ... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Component
struct Child {
// @Consume prohibits local initialization. If the corresponding @Provide cannot be found, an exception is thrown.
@Consume message: string;
build(){
Text(this.message)
}
}
@Entry
@Component
struct Parent {
@Provide message: string = "Hello World";
build(){
Column(){
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @ComponentV2
struct Child {
// @Consumer allows local initialization. Local default value will be used when \@Provider is not found.
@Consumer() message: string = "Hello World";
build(){
Text(this.message)
}
}
@Entry
@ComponentV2
struct Parent {
build(){
Column(){
Child()
}
}
} |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct Parent {
@State parentValue: number = 42;
build() {
Column() {
// @Provide supports initialization from the parent component.
Child({ childValue: this.parentValue })
}
}
}
@Component
struct Child {
@Provide childValue: number = 0;
build(){
Column(){
Text... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@ComponentV2
struct Parent {
@Local parentValue: number = 42;
build() {
Column() {
// @Provider prohibits localization from the parent component. Alternatively, you can use @Param to receive the value and then assign it to @Provider.
Child({ initialValue: this.parentValue })
}
}
}
@Com... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct GrandParent {
@Provide("reviewVotes") reviewVotes: number = 40;
build() {
Column(){
Parent()
}
}
}
@Component
struct Parent {
// @Provide does not support overloading by default. Set the **allowOverride** function to enable.
@Provide({ allowOverride: "reviewVotes" }) re... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@ComponentV2
struct GrandParent {
@Provider("reviewVotes") reviewVotes: number = 40;
build() {
Column(){
Parent()
}
}
}
@ComponentV2
struct Parent {
// @Provider supports overloading by default. @Consumer searches for the nearest @Provider upwards.
@Provider() reviewVotes: number = 20;
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct watchExample {
@State @Watch('onAppleChange') apple: number = 0;
onAppleChange(): void {
console.log("apple count changed to "+this.apple);
}
build() {
Column(){
Text(`apple count: ${this.apple}`)
Button("add apple")
.onClick(() => {
this.apple++;
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@ComponentV2
struct monitorExample {
@Local apple: number = 0;
@Monitor('apple')
onFruitChange(monitor: IMonitor) {
console.log(`apple changed from ${monitor.value()?.before} to ${monitor.value()?.now}`);
}
build() {
Column(){
Text(`apple count: ${this.apple}`)
Button("add apple")
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct watchExample {
@State @Watch('onAppleChange') apple: number = 0;
@State @Watch('onOrangeChange') orange: number = 0;
// @Watch callback, which is used to listen for only a single variable but cannot obtain the value before change.
onAppleChange(): void {
console.log("apple count cha... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@ComponentV2
struct monitorExample {
@Local apple: number = 0;
@Local orange: number = 0;
// @Monitor callback, which is used to listen for multiple variables and obtain the value before change.
@Monitor('apple','orange')
onFruitChange(monitor: IMonitor) {
monitor.dirty.forEach((name: string) => {... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // Page1.ets
export let storage: LocalStorage = new LocalStorage();
storage.setOrCreate('count', 47);
@Entry(storage)
@Component
struct Page1 {
@LocalStorageProp('count') count: number = 0;
pageStack: NavPathStack = new NavPathStack();
build() {
Navigation(this.pageStack) {
Column() {
Text(`${t... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // Page2.ets
import { storage } from './Page1'
@Builder
export function Page2Builder() {
Page2()
}
// The Page2 component obtains the LocalStorage instance of the parent component Page1.
@Component
struct Page2 {
@LocalStorageProp('count') count: number = 0;
pathStack: NavPathStack = new NavPathStack();
build(... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // Page1.ets
import { MyStorage } from './storage';
@Entry
@ComponentV2
struct Page1 {
storage: MyStorage = MyStorage.instance();
pageStack: NavPathStack = new NavPathStack();
@Local count: number = this.storage.count;
@Monitor('storage.count')
onCountChange(mon: IMonitor) {
console.log(`Page1 ${mon.val... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // Page2.ets
import { MyStorage } from './storage';
@Builder
export function Page2Builder() {
Page2()
}
@ComponentV2
struct Page2 {
storage: MyStorage = MyStorage.instance();
pathStack: NavPathStack = new NavPathStack();
@Local count: number = this.storage.count;
@Monitor('storage.count')
onCountChange(m... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | let localStorageA: LocalStorage = new LocalStorage();
localStorageA.setOrCreate('PropA', 'PropA');
let localStorageB: LocalStorage = new LocalStorage();
localStorageB.setOrCreate('PropB', 'PropB');
let localStorageC: LocalStorage = new LocalStorage();
localStorageC.setOrCreate('PropC', 'PropC');
@Entry
@Component
st... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // storage.ets
@ObservedV2
export class MyStorageA {
@Trace propA: string = 'Hello';
constructor(propA?: string) {
this.propA = propA? propA : this.propA;
}
}
@ObservedV2
export class MyStorageB extends MyStorageA {
@Trace propB: string = 'Hello';
constructor(propB: string) {
super();
this.prop... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // Index.ets
import { MyStorageA, MyStorageB, MyStorageC } from './storage';
@Entry
@ComponentV2
struct MyNavigationTestStack {
pageInfo: NavPathStack = new NavPathStack();
@Builder
PageMap(name: string) {
if (name === 'pageOne') {
pageOneStack()
} else if (name === 'pageTwo') {
pageTwoStac... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // EntryAbility Index.ets
import { common, Want } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@StorageProp('count') count: number = 0;
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
build() {
Column() {
Text(`EntryAbility count: ${this.count}`)
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // EntryAbility1 Index1.ets
import { common, Want } from '@kit.AbilityKit';
@Entry
@Component
struct Index1 {
@StorageProp('count') count: number = 0;
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
build() {
Column() {
Text(`EntryAbility1 count: ${this.count}`)
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { common, Want } from '@kit.AbilityKit';
import { AppStorageV2 } from '@kit.ArkUI';
@ObservedV2
export class MyStorage {
@Trace count: number = 0;
}
@Entry
@ComponentV2
struct Index {
@Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!;
@Local count: number = th... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { common, Want } from '@kit.AbilityKit';
import { AppStorageV2 } from '@kit.ArkUI';
@ObservedV2
export class MyStorage {
@Trace count: number = 0;
}
@Entry
@ComponentV2
struct Index1 {
@Local storage: MyStorage = AppStorageV2.connect(MyStorage, 'storage', () => new MyStorage())!;
@Local count: number = t... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | // Save the device language code to AppStorage.
Environment.envProp('languageCode', 'en');
@Entry
@Component
struct Index {
@StorageProp('languageCode') languageCode: string = 'en';
build() {
Row() {
Column() {
// Output the current device language code.
Text(this.languageCode)
}
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | class data {
name: string = 'ZhangSan';
id: number = 0;
}
PersistentStorage.persistProp('numProp', 47);
PersistentStorage.persistProp('dataProp', new data());
@Entry
@Component
struct Index {
@StorageLink('numProp') numProp: number = 48;
@StorageLink('dataProp') dataProp: data = new data();
build() {
C... |
application-dev\ui\state-management\arkts-v1-v2-migration.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}`);
});
class Data {
name: string = 'Zha... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct ListExample {
private arr: Array<number> = new Array(10).fill(0);
private scroller: ListScroller = new ListScroller();
@State listSpace: number = 10;
@State listChildrenSize: ChildrenMainSize = new ChildrenMainSize(100);
build() {
Column() {
Button('change Default').onClick... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { UIUtils } from '@kit.ArkUI';
@Entry
@ComponentV2
struct ListExample {
private arr: Array<number> = new Array(10).fill(0);
private scroller: ListScroller = new ListScroller();
listSpace: number = 10;
// Use the makeObserved capability to observe ChildrenMainSize.
listChildrenSize: ChildrenMainSize = ... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | @Entry
@Component
struct WaterFlowSample {
@State colors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Pink];
@State sections: WaterFlowSections = new WaterFlowSections();
scroller: Scroller = new Scroller();
@State private arr: Array<number> = new Array(9).fill(0);
oneColu... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { UIUtils } from '@kit.ArkUI';
@Entry
@ComponentV2
struct WaterFlowSample {
colors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Pink];
// Use the makeObserved capability to observe WaterFlowSections.
sections: WaterFlowSections = UIUtils.makeObserved(new WaterFlowSecti... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | class MyButtonModifier implements AttributeModifier<ButtonAttribute> {
isDark: boolean = false;
applyNormalAttribute(instance: ButtonAttribute): void {
if (this.isDark) {
instance.backgroundColor(Color.Black);
} else {
instance.backgroundColor(Color.Red);
}
}
}
@Entry
@Component
struct A... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { UIUtils } from '@kit.ArkUI';
class MyButtonModifier implements AttributeModifier<ButtonAttribute> {
isDark: boolean = false;
applyNormalAttribute(instance: ButtonAttribute): void {
if (this.isDark) {
instance.backgroundColor(Color.Black);
} else {
instance.backgroundColor(Color.Red);
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { CommonModifier } from '@ohos.arkui.modifier';
class MyModifier extends CommonModifier {
applyNormalAttribute(instance: CommonAttribute): void {
super.applyNormalAttribute?.(instance);
}
public setGroup1(): void {
this.borderStyle(BorderStyle.Dotted);
this.borderWidth(8);
}
public setGr... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { UIUtils } from '@kit.ArkUI';
import { CommonModifier } from '@ohos.arkui.modifier';
class MyModifier extends CommonModifier {
applyNormalAttribute(instance: CommonAttribute): void {
super.applyNormalAttribute?.(instance);
}
public setGroup1(): void {
this.borderStyle(BorderStyle.Dotted);
th... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { TextModifier } from '@ohos.arkui.modifier';
class MyModifier extends TextModifier {
applyNormalAttribute(instance: TextModifier): void {
super.applyNormalAttribute?.(instance);
}
public setGroup1(): void {
this.fontSize(50);
this.fontColor(Color.Pink);
}
public setGroup2(): void {
... |
application-dev\ui\state-management\arkts-v1-v2-migration.md | import { UIUtils } from '@kit.ArkUI';
import { TextModifier } from '@ohos.arkui.modifier';
class MyModifier extends TextModifier {
applyNormalAttribute(instance: TextModifier): void {
super.applyNormalAttribute?.(instance);
}
public setGroup1(): void {
this.fontSize(50);
this.fontColor(Color.Pink);
... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Observed
class ObservedClass {
}
@Entry
@Component
struct CompV1 {
@State observedClass: ObservedClass = new ObservedClass();
build() {
Column() {
CompV2({ observedClass: UIUtils.enableV2Compatibility(this.observedClass) })
}
}
}
@ComponentV2
struct CompV2 ... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Observed
class ObservedClass {}
@Entry
@ComponentV2
struct CompV2 {
@Local observedClass: ObservedClass = UIUtils.enableV2Compatibility(new ObservedClass());
build() {
Column() {
CompV1({ observedClass: this.observedClass })
}
}
}
@Component
struct CompV1 {
... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | // Recommended. When this.state = new ObservedClass() is called, UIUtils.enableV2Compatibility is not required, simplifying the code.
SubComponentV2({param: UIUtils.enableV2Compatibility(this.state)})
// Not recommended. When a value is assigned to the state as a whole, UIUtils.enableV2Compatibility needs to be called... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | // Recommended.
@Local unObservedClass: UnObservedClass = UIUtils.enableV2Compatibility(UIUtils.makeV1Observed(new UnObservedClass()));
// Recommended. ObservedClass is a class decorated by @Observed.
@Local observedClass: ObservedClass = UIUtils.enableV2Compatibility(new ObservedClass()); |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | let arr: Array<ArrayItem> = UIUtils.enableV2Compatibility(UIUtils.makeV1Observed(new ArrayItem()));
arr.push(new ArrayItem()); // The new data is not a state variable of V1. Therefore, the observation capability of V2 is unavailable.
arr.push(UIUtils.makeV1Observed(new ArrayItem())); // The new data is the state varia... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Observed
class ObservedClass {
name: string = 'Tom';
}
@Entry
@Component
struct CompV1 {
@State observedClass: ObservedClass = new ObservedClass();
build() {
Column() {
Text(`@State observedClass: ${this.observedClass.name}`)
.onClick(() => {
t... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | @Observed
class ObservedClass {
name: string = 'Tom';
}
@Entry
@Component
struct CompV1 {
@State observedClass: ObservedClass = new ObservedClass();
build() {
Column() {
Text(`@State observedClass: ${this.observedClass.name}`)
.onClick(() => {
this.observedClass.name += '!'; // Refre... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
class ObservedClass {
name: string = 'Tom';
}
@Entry
@ComponentV2
struct CompV2 {
@Local observedClass: ObservedClass = UIUtils.enableV2Compatibility(UIUtils.makeV1Observed(new ObservedClass()));
build() {
Column() {
// @Local can only observe itself.
// Ho... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | class ObservedClass {
name: string = 'Tom';
}
@Entry
@ComponentV2
struct CompV2 {
@Local observedClass: ObservedClass = new ObservedClass();
build() {
Column() {
// @Local can only observe itself. Property changes cannot be observed here.
Text(`@Local observedClass: ${this.observedClass.name}`)
... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Observed
class ObservedClass {
@Track name: string = 'a';
count: number = 0;
}
@Entry
@Component
struct CompV1 {
@State observedClass: ObservedClass = new ObservedClass();
build() {
Column() {
Text(`name: ${this.observedClass.name}`).onClick(() => {
// ... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Observed
class ObservedClass {
@Track name: string = 'a';
count: number = 0;
}
@Entry
@ComponentV2
struct CompV1 {
@Local observedClass: ObservedClass = UIUtils.enableV2Compatibility(new ObservedClass());
build() {
Column() {
Text(`name: ${this.observedClass.n... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Entry
@Component
struct ArrayCompV1 {
@State arr: Array<number> = UIUtils.makeV1Observed([1, 2, 3]);
build() {
Column() {
Text(`V1 ${this.arr[0]}`).onClick(() => {
// Click to trigger the changes of ArrayCompV1 and ArrayCompV2.
this.arr[0]++;
... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | @Entry
@Component
struct ArrayCompV1 {
@State arr: Array<number> = [1, 2, 3];
build() {
Column() {
Text(`V1 ${this.arr[0]}`).onClick(() => {
// V1 proxy, which can trigger the refresh of ArrayCompV1 but cannot trigger the refresh of ArrayCompV2.
this.arr[0]++;
})
// After bein... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@Entry
@ComponentV2
struct ArrayCompV2 {
@Local arr: Array<number> = UIUtils.enableV2Compatibility(UIUtils.makeV1Observed([1, 2, 3]));
build() {
Column() {
Text(`V2 ${this.arr[0]}`).fontSize(20).onClick(() => {
// Click to trigger changes in V2 and synchroni... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | @Entry
@ComponentV2
struct ArrayCompV2 {
@Local arr: Array<number> = [1, 2, 3];
build() {
Column() {
Text(`V2 ${this.arr[0]}`).fontSize(20).onClick(() => {
// Click to trigger changes in V2.
this.arr[0]++;
})
// The data passed to @ObjectLink is not @Observed or makeV1Observed... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
@ComponentV2
struct Item {
@Require @Param itemArr: Array<string>;
build() {
Row() {
ForEach(this.itemArr, (item: string, index: number) => {
Text(`${index}: ${item}`)
}, (item: string) => item + Math.random())
Button('@Param push')
.onC... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.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}`)
}, (item: string) => item + Math.random())
Button('@ObjectLink push')
.onCl... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | // Not recommended.
NestedClassV2({ outer: this.outer }) |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
class ArrayItem {
value: number = 0;
constructor(value: number) {
this.value = value;
}
}
class Inner {
innerValue: string = 'inner';
arr: Array<ArrayItem>;
constructor(arr: Array<ArrayItem>) {
this.arr = arr;
}
}
class Outer {
@Track outerValue: string... |
application-dev\ui\state-management\arkts-v1-v2-mixusage.md | import { UIUtils } from '@kit.ArkUI';
class ArrayItem {
value: number = 0;
constructor(value: number) {
this.value = value;
}
}
class Inner {
innerValue: string = 'inner';
arr: Array<ArrayItem>;
constructor(arr: Array<ArrayItem>) {
this.arr = arr;
}
}
class Outer {
@Track outerValue: string... |
application-dev\ui\state-management\arkts-watch.md | // Incorrect format. An error is reported during compilation.
@State @Watch() num: number = 10;
@State @Watch(change) num: number = 10;
// Correct format.
@State @Watch('change') num: number = 10;
change() {
console.log(`xxx`);
} |
application-dev\ui\state-management\arkts-watch.md | // Incorrect format. No function with the corresponding name is available, and an error is reported during compilation.
@State @Watch('change') num: number = 10;
onChange() {
console.log(`xxx`);
}
// Correct format.
@State @Watch('change') num: number = 10;
change() {
console.log(`xxx`);
} |
application-dev\ui\state-management\arkts-watch.md | // Incorrect format.
@Watch('change') num: number = 10;
change() {
console.log(`xxx`);
}
// Correct format.
@State @Watch('change') num: number = 10;
change() {
console.log(`xxx`);
} |
application-dev\ui\state-management\arkts-watch.md | @Component
struct TotalView {
@Prop @Watch('onCountUpdated') count: number = 0;
@State total: number = 0;
// @Watch callback
onCountUpdated(propName: string): void {
this.total += this.count;
}
build() {
Text(`Total: ${this.total}`)
}
}
@Entry
@Component
struct CountModifier {
@State count: nu... |
application-dev\ui\state-management\arkts-watch.md | class PurchaseItem {
static NextId: number = 0;
public id: number;
public price: number;
constructor(price: number) {
this.id = PurchaseItem.NextId++;
this.price = price;
}
}
@Component
struct BasketViewer {
@Link @Watch('onBasketUpdated') shopBasket: PurchaseItem[];
@State totalPurchase: number... |
application-dev\ui\state-management\arkts-watch.md | @Observed
class Task {
isFinished: boolean = false;
constructor(isFinished : boolean) {
this.isFinished = isFinished;
}
}
@Entry
@Component
struct ParentComponent {
@State @Watch('onTaskAChanged') taskA: Task = new Task(false);
@State @Watch('onTaskBChanged') taskB: Task = new Task(false);
onTaskACha... |
application-dev\ui\state-management\arkts-watch.md | @Entry
@Component
struct UsePropertyName {
@State @Watch('countUpdated') apple: number = 0;
@State @Watch('countUpdated') cabbage: number = 0;
@State fruit: number = 0;
// @Watch callback
countUpdated(propName: string): void {
if (propName == 'apple') {
this.fruit = this.apple;
}
}
build() ... |
application-dev\ui\state-management\arkts-wrapBuilder.md | @Builder
function builderElement() {}
let builderArr: Function[] = [builderElement];
@Builder
function testBuilder() {
ForEach(builderArr, (item: Function) => {
item();
})
} |
application-dev\ui\state-management\arkts-wrapBuilder.md | declare function wrapBuilder< Args extends Object[]>(builder: (...args: Args) => void): WrappedBuilder; |
application-dev\ui\state-management\arkts-wrapBuilder.md | declare class WrappedBuilder< Args extends Object[]> {
builder: (...args: Args) => void;
constructor(builder: (...args: Args) => void);
} |
application-dev\ui\state-management\arkts-wrapBuilder.md | let builderVar: WrappedBuilder<[string, number]> = wrapBuilder(MyBuilder)
let builderArr: WrappedBuilder<[string, number]>[] = [wrapBuilder(MyBuilder)] // An array is acceptable. |
application-dev\ui\state-management\arkts-wrapBuilder.md | @Builder
function MyBuilder(value: string, size: number) {
Text(value)
.fontSize(size)
}
let globalBuilder: WrappedBuilder<[string, number]> = wrapBuilder(MyBuilder);
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
Row() {
Column() {
globalBuilder.builde... |
application-dev\ui\state-management\arkts-wrapBuilder.md | class Tmp {
paramA2: string = 'hello';
}
@Builder function overBuilder(param: Tmp) {
Column(){
Text(`wrapBuildervalue:${param.paramA2}`)
}
}
const wBuilder: WrappedBuilder<[Tmp]> = wrapBuilder(overBuilder);
@Entry
@Component
struct Parent{
@State label: Tmp = new Tmp();
build(){
Column(){
wBu... |
application-dev\ui\state-management\arkts-wrapBuilder.md | @Builder
function MyBuilderFirst(value: string, size: number) {
Text('MyBuilderFirst: ' + value)
.fontSize(size)
}
@Builder
function MyBuilderSecond(value: string, size: number) {
Text('MyBuilderSecond: ' + value)
.fontSize(size)
}
interface BuilderModel {
globalBuilder: WrappedBuilder<[string, number]>... |
application-dev\ui\state-management\properly-use-state-management-to-develope.md | @Observed
class UIStyle {
@Track translateX: number = 0;
@Track translateY: number = 0;
@Track scaleX: number = 0.3;
@Track scaleY: number = 0.3;
@Track width: number = 336;
@Track height: number = 178;
@Track posX: number = 10;
@Track posY: number = 50;
@Track alpha: number = 0.5;
@Track borderRadi... |
application-dev\web\app-takeovers-web-media.md | // xxx.ets
import { webview } from '@kit.ArkWeb';
@Entry
@Component
struct WebComponent {
controller: webview.WebviewController = new webview.WebviewController();
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
.enableNativeMediaPlayer({ enable... |
application-dev\web\app-takeovers-web-media.md | // xxx.ets
import { webview } from '@kit.ArkWeb';
// Implement the webview.NativeMediaPlayerBridge API.
// The ArkWeb kernel calls the webview.NativeMediaPlayerBridge methods to control playback on NativeMediaPlayer.
class NativeMediaPlayerImpl implements webview.NativeMediaPlayerBridge {
// ...Implement t... |
application-dev\web\app-takeovers-web-media.md | // xxxAbility.ets
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
windowStage.loadContent('pages/Index', (err, data) => {
if (err.code) {
... |
application-dev\web\app-takeovers-web-media.md | // xxx.ets
import { webview } from '@kit.ArkWeb';
import { BuilderNode, FrameNode, NodeController, NodeRenderType } from '@kit.ArkUI';
interface ComponentParams {}
class MyNodeController extends NodeController {
private rootNode: BuilderNode<[ComponentParams]> | undefined;
constructor(surfaceId... |
application-dev\web\app-takeovers-web-media.md | // xxx.ets
import { webview } from '@kit.ArkWeb';
class ActualNativeMediaPlayerListener {
constructor(handler: webview.NativeMediaPlayerHandler) {}
}
class NativeMediaPlayerImpl implements webview.NativeMediaPlayerBridge {
constructor(handler: webview.NativeMediaPlayerHandler, mediaInfo: webview.Media... |
application-dev\web\app-takeovers-web-media.md | // xxx.ets
import { webview } from '@kit.ArkWeb';
class ActualNativeMediaPlayerListener {
handler: webview.NativeMediaPlayerHandler;
constructor(handler: webview.NativeMediaPlayerHandler) {
this.handler = handler;
}
onPlaying() {
// The native media player starts playback.
this.... |
application-dev\web\app-takeovers-web-media.md | "ohos.permission.INTERNET" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.