未验证 提交 0e77f3d0 编写于 作者: 耿文广 提交者: Gitee

update zh-cn/application-dev/quick-start/arkts-state-management-best-practices.md.

Signed-off-by: N耿文广 <gengwenguang@huawei.com>
上级 aa86c2ec
...@@ -41,7 +41,7 @@ struct LinkChild { ...@@ -41,7 +41,7 @@ struct LinkChild {
@Component @Component
struct PropChild2 { struct PropChild2 {
@Prop testNum: ClassA; @Prop testNum: ClassA = new ClassA(0);
build() { build() {
Text(`PropChild2 testNum ${this.testNum.c}`) Text(`PropChild2 testNum ${this.testNum.c}`)
...@@ -53,7 +53,7 @@ struct PropChild2 { ...@@ -53,7 +53,7 @@ struct PropChild2 {
@Component @Component
struct PropChild3 { struct PropChild3 {
@Prop testNum: ClassA; @Prop testNum: ClassA = new ClassA(0);
build() { build() {
Text(`PropChild3 testNum ${this.testNum.c}`) Text(`PropChild3 testNum ${this.testNum.c}`)
...@@ -130,23 +130,23 @@ class ClassA { ...@@ -130,23 +130,23 @@ class ClassA {
@Component @Component
struct LinkChild { struct LinkChild {
@Link testNum: ClassA; @Link testNum: ClassA;
build() { build() {
Text(`LinkChild testNum ${this.testNum?.c}`) Text(`LinkChild testNum ${this.testNum?.c}`)
} }
} }
@Component @Component
struct PropChild1 { struct PropChild1 {
@Prop testNum: ClassA = new ClassA(1); @Prop testNum: ClassA = new ClassA(1);
build() { build() {
Text(`PropChild1 testNum ${this.testNum?.c}`) Text(`PropChild1 testNum ${this.testNum?.c}`)
.onClick(() => { .onClick(() => {
this.testNum = new ClassA(48); this.testNum = new ClassA(48);
}) })
} }
} }
@Component @Component
...@@ -350,44 +350,44 @@ class ClassB extends ClassA { ...@@ -350,44 +350,44 @@ class ClassB extends ClassA {
@Component @Component
struct ViewClassC { struct ViewClassC {
@ObjectLink c : ClassC; @ObjectLink c : ClassC;
build() { build() {
Column({space:10}) { Column({space:10}) {
Text(`c: ${this.c.getC()}`) Text(`c: ${this.c.getC()}`)
Button("Change C") Button("Change C")
.onClick(() => { .onClick(() => {
this.c.setC(this.c.getC()+1); this.c.setC(this.c.getC()+1);
}) })
}
} }
}
} }
@Entry @Entry
@Component @Component
struct MyView { struct MyView {
@State b : ClassB = new ClassB(10, 20, 30); @State b : ClassB = new ClassB(10, 20, 30);
build() { build() {
Column({space:10}) { Column({space:10}) {
Text(`a: ${this.b.a}`) Text(`a: ${this.b.a}`)
Button("Change ClassA.a") Button("Change ClassA.a")
.onClick(() => { .onClick(() => {
this.b.a +=1; this.b.a +=1;
}) })
Text(`b: ${this.b.b}`) Text(`b: ${this.b.b}`)
Button("Change ClassB.b") Button("Change ClassB.b")
.onClick(() => { .onClick(() => {
this.b.b += 1; this.b.b += 1;
}) })
ViewClassC({c: this.b.c}) // Text(`c: ${this.b.c.c}`)的替代写法 ViewClassC({c: this.b.c}) // Text(`c: ${this.b.c.c}`)的替代写法
Button("Change ClassB.ClassC.c") Button("Change ClassB.ClassC.c")
.onClick(() => { .onClick(() => {
this.b.c.c += 1; this.b.c.c += 1;
}) })
} }
} }
} }
``` ```
...@@ -460,10 +460,10 @@ struct ParentComp { ...@@ -460,10 +460,10 @@ struct ParentComp {
CounterComp({ value: this.counter[2] }) CounterComp({ value: this.counter[2] })
Divider().height(5) Divider().height(5)
ForEach(this.counter, ForEach(this.counter,
item => { (item: ParentCounter) => {
CounterComp({ value: item }) CounterComp({ value: item })
}, },
item => item.id.toString() (item: ParentCounter) => item.id.toString()
) )
Divider().height(5) Divider().height(5)
// 第一个点击事件 // 第一个点击事件
...@@ -571,15 +571,15 @@ struct ParentComp { ...@@ -571,15 +571,15 @@ struct ParentComp {
build() { build() {
Row() { Row() {
Column() { Column() {
CounterComp({ value: this.counter[0], subValue: this.counter[0].subCounter }) CounterComp({ value: this.counter[0], subValue: this.counter[0].subCounter[0] })
CounterComp({ value: this.counter[1], subValue: this.counter[1].subCounter }) CounterComp({ value: this.counter[1], subValue: this.counter[1].subCounter[0] })
CounterComp({ value: this.counter[2], subValue: this.counter[2].subCounter }) CounterComp({ value: this.counter[2], subValue: this.counter[2].subCounter[0] })
Divider().height(5) Divider().height(5)
ForEach(this.counter, ForEach(this.counter,
item => { (item: ParentCounter) => {
CounterComp({ value: item, subValue: item.subCounter }) CounterComp({ value: item, subValue: item.subCounter[0] })
}, },
item => item.id.toString() (item: ParentCounter) => item.id.toString()
) )
Divider().height(5) Divider().height(5)
Text('Parent: reset entire counter') Text('Parent: reset entire counter')
...@@ -668,15 +668,15 @@ struct ParentComp { ...@@ -668,15 +668,15 @@ struct ParentComp {
build() { build() {
Row() { Row() {
Column() { Column() {
CounterComp({ value: this.counter[0], subValue: this.counter[0].subCounter }) CounterComp({ value: this.counter[0], subValue: this.counter[0].subCounter[0] })
CounterComp({ value: this.counter[1], subValue: this.counter[1].subCounter }) CounterComp({ value: this.counter[1], subValue: this.counter[1].subCounter[0] })
CounterComp({ value: this.counter[2], subValue: this.counter[2].subCounter }) CounterComp({ value: this.counter[2], subValue: this.counter[2].subCounter[0] })
Divider().height(5) Divider().height(5)
ForEach(this.counter, ForEach(this.counter,
item => { (item: ParentCounter) => {
CounterComp({ value: item, subValue: item.subCounter }) CounterComp({ value: item, subValue: item.subCounter[0] })
}, },
item => item.id.toString() (item: ParentCounter) => item.id.toString()
) )
Divider().height(5) Divider().height(5)
Text('Parent: reset entire counter') Text('Parent: reset entire counter')
...@@ -754,6 +754,37 @@ struct CounterComp { ...@@ -754,6 +754,37 @@ struct CounterComp {
```ts ```ts
let nextId = 1;
@Observed
class SubCounter {
counter: number;
constructor(c: number) {
this.counter = c;
}
}
@Observed
class ParentCounter {
id: number;
counter: number;
subCounter: SubCounter;
incrCounter() {
this.counter++;
}
incrSubCounter(c: number) {
this.subCounter.counter += c;
}
setSubCounter(c: number): void {
this.subCounter.counter = c;
}
constructor(c: number) {
this.id = nextId++;
this.counter = c;
this.subCounter = new SubCounter(c);
}
}
@Component @Component
struct SubCounterComp { struct SubCounterComp {
@ObjectLink subValue: SubCounter; @ObjectLink subValue: SubCounter;
...@@ -767,7 +798,7 @@ struct SubCounterComp { ...@@ -767,7 +798,7 @@ struct SubCounterComp {
} }
@Component @Component
struct CounterComp { struct CounterComp {
@Prop value: ParentCounter; @ObjectLink value: ParentCounter;
build() { build() {
Column({ space: 10 }) { Column({ space: 10 }) {
Text(`this.value.incrCounter(): this.value.counter: ${this.value.counter}`) Text(`this.value.incrCounter(): this.value.counter: ${this.value.counter}`)
...@@ -798,10 +829,10 @@ struct ParentComp { ...@@ -798,10 +829,10 @@ struct ParentComp {
CounterComp({ value: this.counter[2] }) CounterComp({ value: this.counter[2] })
Divider().height(5) Divider().height(5)
ForEach(this.counter, ForEach(this.counter,
item => { (item: ParentCounter) => {
CounterComp({ value: item }) CounterComp({ value: item })
}, },
item => item.id.toString() (item: ParentCounter) => item.id.toString()
) )
Divider().height(5) Divider().height(5)
Text('Parent: reset entire counter') Text('Parent: reset entire counter')
...@@ -949,15 +980,18 @@ struct CompA { ...@@ -949,15 +980,18 @@ struct CompA {
realState1: Array<number> = [4, 1, 3, 2]; // 未使用状态变量装饰器 realState1: Array<number> = [4, 1, 3, 2]; // 未使用状态变量装饰器
realState2: Color = Color.Yellow; realState2: Color = Color.Yellow;
updateUI(param: any): any { updateUI1(param: Array<number>): Array<number> {
const triggerAGet = this.needsUpdate;
return param;
}
updateUI2(param: Color): Color {
const triggerAGet = this.needsUpdate; const triggerAGet = this.needsUpdate;
return param; return param;
} }
build() { build() {
Column({ space: 20 }) { Column({ space: 20 }) {
ForEach(this.updateUI(this.realState1), ForEach(this.updateUI1(this.realState1),
item => { (item: Array<number>) => {
Text(`${item}`) Text(`${item}`)
}) })
Text("add item") Text("add item")
...@@ -976,7 +1010,7 @@ struct CompA { ...@@ -976,7 +1010,7 @@ struct CompA {
// 触发UI视图更新 // 触发UI视图更新
this.needsUpdate = !this.needsUpdate; this.needsUpdate = !this.needsUpdate;
}) })
}.backgroundColor(this.updateUI(this.realState2)) }.backgroundColor(this.updateUI2(this.realState2))
.width(200).height(500) .width(200).height(500)
} }
} }
...@@ -1005,7 +1039,7 @@ struct CompA { ...@@ -1005,7 +1039,7 @@ struct CompA {
build() { build() {
Column({ space: 20 }) { Column({ space: 20 }) {
ForEach(this.realState1, ForEach(this.realState1,
item => { (item: Array<number>) => {
Text(`${item}`) Text(`${item}`)
}) })
Text("add item") Text("add item")
...@@ -1084,12 +1118,6 @@ class ClassE { ...@@ -1084,12 +1118,6 @@ class ClassE {
this.d = d; this.d = d;
} }
} }
```
以下组件层次结构呈现的是@Prop嵌套场景的数据结构。
```ts
@Entry @Entry
@Component @Component
struct Parent { struct Parent {
...@@ -1108,7 +1136,7 @@ struct Parent { ...@@ -1108,7 +1136,7 @@ struct Parent {
@Component @Component
struct Child { struct Child {
@Prop voteOne: ClassE @ObjectLink voteOne: ClassE
build() { build() {
Column() { Column() {
Text(this.voteOne.name).fontSize(24).fontColor(Color.Red).margin(50) Text(this.voteOne.name).fontSize(24).fontColor(Color.Red).margin(50)
...@@ -1123,7 +1151,7 @@ struct Child { ...@@ -1123,7 +1151,7 @@ struct Child {
@Component @Component
struct ChildOne { struct ChildOne {
@Prop voteTwo: ClassD @ObjectLink voteTwo: ClassD
build() { build() {
Column() { Column() {
Text(this.voteTwo.name).fontSize(24).fontColor(Color.Red).margin(50) Text(this.voteTwo.name).fontSize(24).fontColor(Color.Red).margin(50)
...@@ -1138,7 +1166,7 @@ struct ChildOne { ...@@ -1138,7 +1166,7 @@ struct ChildOne {
@Component @Component
struct ChildTwo { struct ChildTwo {
@Prop voteThree: ClassC @ObjectLink voteThree: ClassC
build() { build() {
Column() { Column() {
Text(this.voteThree.name).fontSize(24).fontColor(Color.Red).margin(50) Text(this.voteThree.name).fontSize(24).fontColor(Color.Red).margin(50)
...@@ -1153,7 +1181,7 @@ struct ChildTwo { ...@@ -1153,7 +1181,7 @@ struct ChildTwo {
@Component @Component
struct ChildThree { struct ChildThree {
@Prop voteFour: ClassB @ObjectLink voteFour: ClassB
build() { build() {
Column() { Column() {
Text(this.voteFour.name).fontSize(24).fontColor(Color.Red).margin(50) Text(this.voteFour.name).fontSize(24).fontColor(Color.Red).margin(50)
...@@ -1168,7 +1196,7 @@ struct ChildThree { ...@@ -1168,7 +1196,7 @@ struct ChildThree {
@Component @Component
struct ChildFour { struct ChildFour {
@Prop voteFive: ClassA @ObjectLink voteFive: ClassA
build() { build() {
Column() { Column() {
Text(this.voteFive.title).fontSize(24).fontColor(Color.Red).margin(50) Text(this.voteFive.title).fontSize(24).fontColor(Color.Red).margin(50)
...@@ -1245,6 +1273,59 @@ class ClassE { ...@@ -1245,6 +1273,59 @@ class ClassE {
以下组件层次结构呈现的是@Reusable组件复用场景的数据结构。 以下组件层次结构呈现的是@Reusable组件复用场景的数据结构。
```ts ```ts
// 以下是嵌套类对象的数据结构。
@Observed
class ClassA {
public title: string;
constructor(title: string) {
this.title = title;
}
}
@Observed
class ClassB {
public name: string;
public a: ClassA;
constructor(name: string, a: ClassA) {
this.name = name;
this.a = a;
}
}
@Observed
class ClassC {
public name: string;
public b: ClassB;
constructor(name: string, b: ClassB) {
this.name = name;
this.b = b;
}
}
@Observed
class ClassD {
public name: string;
public c: ClassC;
constructor(name: string, c: ClassC) {
this.name = name;
this.c = c;
}
}
@Observed
class ClassE {
public name: string;
public d: ClassD;
constructor(name: string, d: ClassD) {
this.name = name;
this.d = d;
}
}
@Entry @Entry
@Component @Component
struct Parent { struct Parent {
...@@ -1266,9 +1347,9 @@ struct Parent { ...@@ -1266,9 +1347,9 @@ struct Parent {
@Component @Component
struct Child { struct Child {
@State voteOne: ClassE = new ClassE('voteOne', new ClassD('OpenHarmony', new ClassC('Hello', new ClassB('World', new ClassA('Peace'))))) @State voteOne: ClassE = new ClassE('voteOne', new ClassD('OpenHarmony', new ClassC('Hello', new ClassB('World', new ClassA('Peace')))))
aboutToReuse(params){
this.voteOne = params
aboutToReuse(params: ClassE) {
this.voteOne = params
} }
build() { build() {
Column() { Column() {
...@@ -1287,7 +1368,7 @@ struct Child { ...@@ -1287,7 +1368,7 @@ struct Child {
@Component @Component
struct ChildOne { struct ChildOne {
@State voteTwo: ClassD = new ClassD('voteTwo', new ClassC('Hello', new ClassB('World', new ClassA('Peace')))) @State voteTwo: ClassD = new ClassD('voteTwo', new ClassC('Hello', new ClassB('World', new ClassA('Peace'))))
aboutToReuse(params){ aboutToReuse(params: ClassD){
this.voteTwo = params this.voteTwo = params
} }
build() { build() {
...@@ -1307,7 +1388,7 @@ struct ChildOne { ...@@ -1307,7 +1388,7 @@ struct ChildOne {
@Component @Component
struct ChildTwo { struct ChildTwo {
@State voteThree: ClassC = new ClassC('voteThree', new ClassB('World', new ClassA('Peace'))) @State voteThree: ClassC = new ClassC('voteThree', new ClassB('World', new ClassA('Peace')))
aboutToReuse(params){ aboutToReuse(params: ClassC){
this.voteThree = params this.voteThree = params
} }
...@@ -1328,7 +1409,7 @@ struct ChildTwo { ...@@ -1328,7 +1409,7 @@ struct ChildTwo {
@Component @Component
struct ChildThree { struct ChildThree {
@State voteFour: ClassB = new ClassB('voteFour', new ClassA('Peace')) @State voteFour: ClassB = new ClassB('voteFour', new ClassA('Peace'))
aboutToReuse(params){ aboutToReuse(params: ClassB){
this.voteFour = params this.voteFour = params
} }
...@@ -1349,7 +1430,7 @@ struct ChildThree { ...@@ -1349,7 +1430,7 @@ struct ChildThree {
@Component @Component
struct ChildFour { struct ChildFour {
@State voteFive: ClassA = new ClassA('voteFive') @State voteFive: ClassA = new ClassA('voteFive')
aboutToReuse(params){ aboutToReuse(params: ClassA){
this.voteFive = params this.voteFive = params
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册