鸿蒙开发中bindPopup 不显示内容?

鸿蒙开发中bindPopup 不显示内容?通过 bindPopup 绑定气泡提示,第一次点击可以弹窗 popup,之后无论怎么点击都无法弹出。
场景描述:无法提供最小化demo,经过 demo 测试,在渲染多个 popup 时频繁切换点击触发后,会偶现点击不显示的问题。
代码如下:

@Component 
export struct PopupItem { 
  @State handlePopup: boolean = false 
  build() { 
    Button('PopupOptions') 
      .onClick(() => { 
        this.handlePopup = !this.handlePopup 
      }) 
      .bindPopup(this.handlePopup, { 
        width: 300, 
        message: 'This is a popup with PopupOptions', 
        arrowPointPosition: ArrowPointPosition.START, // 设置箭头的位置 
        backgroundBlurStyle: BlurStyle.NONE, // 关闭气泡的模糊背景 
        popupColor: Color.Red, // 设置气泡的背景色 
        autoCancel: true, 
      }) 
  } 
} 
 
 
@Component 
export struct PopupTest { 
  @State list: number[] = [1, 2, 3, 4, 5] 
 
  build() { 
    Column({ space: 20 }) { 
      ForEach(this.list, () => { 
        PopupItem() 
      }) 
    } 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

阅读 477
avatarAI BotBETA
问题原因可能是多个 `PopupItem` 组件共享了相同的 `handlePopup` 状态,导致状态管理混乱。在鸿蒙开发中,每个 `PopupItem` 组件应该有自己的独立状态来控制其 `Popup` 的显示与隐藏。

解决方案是修改 `PopupItem` 组件,使其能够接收一个外部控制参数(比如 `showPopup`),并在每个 `PopupItem` 实例中维护一个独立的内部状态。这样,每个按钮点击时只会影响它自己的 `Popup` 状态。

修改后的代码如下:

@Component
export struct PopupItem {
@Prop showPopup: boolean = false;
@State internalPopupState: boolean = false;

@Watch('showPopup')
showPopupChanged(newValue: boolean) {

this.internalPopupState = newValue;

}

build() {

Button('PopupOptions')
  .onClick(() => {
    this.internalPopupState = !this.internalPopupState;
    // 通知父组件更新 showPopup,如果需要的话(这里取决于具体的设计需求)
  })
  .bindPopup(this.internalPopupState, {
    width: 300,
    message: 'This is a popup with PopupOptions',
    arrowPointPosition: ArrowPointPosition.START,
    backgroundBlurStyle: BlurStyle.NONE,
    popupColor: Color.Red,
    autoCancel: true,
  });

}
}

@Component
export struct PopupTest {
@State list: { id: number, show: boolean }[] = [

{ id: 1, show: false },
{ id: 2, show: false },
{ id: 3, show: false },
{ id: 4, show: false },
{ id: 5, show: false }

];

handlePopupToggle(id: number) {

this.list = this.list.map(item => ({
  ...item,
  show: item.id === id ? !item.show : item.show
}));

}

build() {

Column({ space: 20 }) {
  ForEach(this.list, (item) => {
    PopupItem({ showPopup: item.show })
      .onClick(() => this.handlePopupToggle(item.id)); // 这里假设需要外部控制显示状态
  })
}

}
}


**注意**:上面的代码示例中,`PopupItem` 组件的 `onClick` 方法直接修改了 `internalPopupState`,并且假设了一个 `onClick` 属性传递给 `PopupItem` 来处理外部点击事件(这在实际代码中可能需要根据具体情况调整,因为鸿蒙组件框架可能不支持直接这样传递事件处理函数)。更常见的情况是,`PopupItem` 组件内部只管理自己的状态,并通过父组件传递的 prop 来初始化或控制显示状态。如果需要在父组件中控制多个子组件的 `Popup` 显示,应该在父组件中维护这些状态,并通过 prop 传递给子组件。

此外,上面的代码示例中 `ForEach` 循环和事件处理可能需要根据鸿蒙开发框架的实际 API 进行调整,因为这里只是提供了一个思路,并没有直接使用鸿蒙开发框架的具体 API。
1 个回答

具体解决方案:

@Entry 
@Component 
export struct PopupTest { 
  @State list: number[] = [1, 2, 3, 4, 5] 
  @State isHoverText: Array<boolean> = new Array<boolean>(this.list.length).fill(false) 
  // @State handlePopup: boolean = false 
  build() { 
    Column({ space: 20 }) { 
      ForEach(this.list, (item: number, index: number) => { 
        Button('PopupOption' + item) 
          .onClick(()=>{ 
 
            this.isHoverText[index] = !this.isHoverText[index] 
          }) 
          .bindPopup(this.isHoverText[index], { 
            width: 300, 
            message: 'This is a popup with PopupOptions', 
            arrowPointPosition: ArrowPointPosition.START, // 设置箭头的位置 
            backgroundBlurStyle: BlurStyle.NONE, // 关闭气泡的模糊背景 
            popupColor: Color.Red, // 设置气泡的背景色 
            autoCancel: true, 
            onStateChange: (e) => { 
              console.info(JSON.stringify(e.isVisible)) 
              if (!e.isVisible) { 
                this.isHoverText[index] = false 
              } 
            } 
          }) 
      }) 
    } 
  } 
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题