harmony(鸿蒙)Component Creation and Re-initialization
Component Creation and Re-initialization
Initial Creation and Rendering
Create the parent component ParentComp.
Locally initialize the state variable isCountDown of ParentComp.
Execute the build function of ParentComp.
Create a preset <Column> component.
- Create a preset <Text> component, set the text content to be displayed, and add the <Text> component instance to the <Column> component.
- Create the component on the true branch based on the if condition.
- Create a preset <Image> component and set the image source address.
- Create a TimerComponent using the given constructor.
- Create a TimerComponent object.
- Initialize the values of member variables locally.
- Use the parameters provided by the TimerComponent constructor to update the values of member variables.
- Execute the aboutToAppear function of TimerComponent.
- Execute the build function of TimerComponent to create the corresponding UI description structure.
- Create a preset <Button> component and set the corresponding content.
Status Update
When a user clicks a button:
The value of the isCountDown state variable of ParentComp is changed to false.
The build function of ParentComp is executed.
The preset <Column> component is reused by the framework and reinitialized.
The child components of <Column> reuse and reinitialize the objects in the memory.
- Reuse the preset <Text> component after re-initializing the component using new text content.
- Reuse the component on the false branch based on the if condition.
- Destroy the components on the original true branch as these components are no longer used.
- Destroy the created preset <image> component instance.
- Destroy the TimerComponent component instance, and call the aboutToDisappear function.
- Create components on the false branch.
- Create a preset <Image> component and set the image source address.
- Create a TimerComponent again using the given constructor.
- Initialize the newly created TimerComponent and call the aboutToAppear and build functions.
- Destroy the components on the original true branch as these components are no longer used.
- Reuse the preset <Button> component, with the new image source address.
Example
// xxx.ets
@Entry
@Component
struct ParentComp {
@State isCountDown: boolean = true
build() {
Column() {
Text(this.isCountDown ? 'Count Down' : 'Stopwatch')
if (this.isCountDown) {
Image('countdown.png')
TimerComponent({counter: 10, changePerSec: -1, showInColor: Color.Red})
} else {
Image('stopwatch.png')
TimerComponent({counter: 0, changePerSec: +1, showInColor: Color.Black })
}
Button(this.isCountDown ? 'Switch to Stopwatch' : 'Switch to Count Down')
.onClick(() => {this.isCountDown = !this.isCountDown})
}
}
}
// Manage and display a count down / stop watch timer
@Component
struct TimerComponent {
@State counter: number = 0
private changePerSec: number = -1
private showInColor: Color = Color.Black
private timerId : number = -1
build() {
Text(`${this.counter}sec`)
.fontColor(this.showInColor)
}
aboutToAppear() {
this.timerId = setInterval(() => {this.counter += this.changePerSec}, 1000)
}
aboutToDisappear() {
if (this.timerId > 0) {
clearTimeout(this.timerId)
this.timerId = -1
}
}
}
你可能感兴趣的鸿蒙文章
harmony(鸿蒙)Multi-Language Capability
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦