HarmonyOS Button上使用bindPopup,里面是一个自定义的组件展示内容,展示内容怎么能根据外部的变量动态展示呢

Button上使用bindPopup,里面是一个自定义的组件展示内容,请问展示内容怎么能根据外部的变量动态展示呢

HarmonyOS
2024-12-20 17:03:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

因为bindPopup里非@component的子组件,所以无法使用@state和@link声明式变量进行传递和更新。可以使用AppStorage,对某一个收藏夹的是否公开属性进行新建或更新,使用为AppStorage.SetOrCreate(‘album_id’, this.pub ? ‘0’ : “1”),然后在实现bindPopup时通过声明@StorageLink(‘Album_id’) pub:string = ‘0’ 进行同步更新,获取数据并使用。

Demo:

export enum PopoverItemType {
  Default,
  AddVideo,
  ManageVideo,
  ModifyName,
  ModifyPrivacy,
  DeleteFolder
}

export interface PopoverItem {
  title: string
  type: PopoverItemType
}


@Component
export struct CollectionManageMenu {
  @State popoverItemList1: PopoverItem[] = []
  @State popoverItemList2: PopoverItem[] = []
  @StorageLink('pub') pub: string = '0'


  aboutToAppear(): void {
    this.popoverItemList1 = [
      {
        title: '添加视频',
        type: PopoverItemType.AddVideo
      },
      {
        title: '批量管理视频',
        type: PopoverItemType.ManageVideo
      },
      {
        title: '修改名称',
        type: PopoverItemType.ModifyName
      },
      {
        //TODO:文案要根据状态动态改变
        title: '设置为私密',
        type: PopoverItemType.ModifyPrivacy
      },
      {
        title: '删除收藏夹',
        type: PopoverItemType.DeleteFolder
      }
    ]

    this.popoverItemList2 = [
      {
        title: '添加视频',
        type: PopoverItemType.AddVideo
      },
      {
        title: '批量管理视频',
        type: PopoverItemType.ManageVideo
      },
      {
        title: '修改名称',
        type: PopoverItemType.ModifyName
      },
      {
        //TODO:文案要根据状态动态改变
        title: '设置为公开',
        type: PopoverItemType.ModifyPrivacy
      },
      {
        title: '删除收藏夹',
        type: PopoverItemType.DeleteFolder
      }
    ]
  }

  build() {
    Column() {
      List() {
        ForEach(this.pub == '0' ? this.popoverItemList1 : this.popoverItemList2, (item: PopoverItem, index) => {
          ListItem() {
            Text(item.title)
          }
          .height(52)
          .padding({
            left: 16,
            right: 16,
            top: 14,
            bottom: 14
          })
          .onClick(() => {

          })
        }, (item: PopoverItem) => JSON.stringify(item.title))
      }
      .width('100%')
      .scrollBar(BarState.Off)
    }
    .width(161)
    .alignItems(HorizontalAlign.Center)
    .borderRadius(8)
  }
}

@Entry
@Component
export struct CollectionDetailPageNavBar {
  @State showPopover: boolean = false
  @State pub: boolean = true
  @Builder
  manageMenu() {
    CollectionManageMenu()
  }

  build() {
    Column() {
      Button('管理')
        .width(62)
        .height(32)
        .borderRadius(16)
        .fontSize(15)
        .onClick(() => {
          this.showPopover = !this.showPopover
        })
        .bindPopup(
          this.showPopover,
          {
            builder: () => {
              this.manageMenu()
            },
            radius: 8,
            enableArrow: true,
            placement: Placement.BottomLeft,
            targetSpace: 20,
            offset: { x: -6 },
            onStateChange: (event) => {
              if (!event.isVisible) {
                this.showPopover = false
              }
            }
          }
        )

      Button('改变私密/公开状态')
        .width(62)
        .height(32)
        .borderRadius(16)
        .fontSize(15)
        .onClick(() => {
          this.pub = !this.pub
          AppStorage.SetOrCreate('pub', this.pub ? '0' : "1")
        })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
分享
微博
QQ
微信
回复
2024-12-20 18:39:32
相关问题
HarmonyOS RichText展示内容文字都很小
508浏览 • 1回复 待解决
HarmonyOS 自定义Refresh头部展示不全
586浏览 • 1回复 待解决
如何展示一个贴片广告
1098浏览 • 1回复 待解决
如何展示一个原生广告
979浏览 • 1回复 待解决
如何展示一个插屏广告
1429浏览 • 1回复 待解决
如何展示一个激励广告
1235浏览 • 1回复 待解决
如何展示一个开屏广告
1309浏览 • 1回复 待解决
如何展示一个banner广告
1193浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
1876浏览 • 1回复 待解决