智能客服
你问我答,随时在线为你解决问题
具体可参考示例代码:
@Component
struct ComponentA {
aboutToAppear(): void {
// Perception components are visible and hidden
console.log('Component A display');
}
aboutToDisappear(): void {
// Perception components are visible and hidden
console.log('Component A hidden');
}
build() {
Column() {
Text('Component A').fontSize(16).fontColor(Color.Black);
}
.width(100)
.height(50)
}
}
@Entry
@Component
struct ComponentB {
@State @Watch('onCompAShowStatusChange') isShowA: boolean = false;
onCompAShowStatusChange() {
// Perception components are visible and hidden
console.log('Monitor component A:' + `${this.isShowA ? 'display' : 'hide'}`);
}
build() {
Column() {
Button('Switch between visible and hidden').type(ButtonType.Normal).width(100).height(50).onClick(() => {
this.isShowA = !this.isShowA;
})
if (this.isShowA) {
ComponentA();
}
}
}
} 参考链接