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