HarmonyOS promptAction的buttons如何动态传值?

以下代码报错类型匹配不上。

let shares = ["分享1","分享2"] //模拟接口返回动态数据  
let buttons = shares?.map((value) => {  
  let button: Button = {  
    text: value,  
    color: '#333333'  
  }  
  return button  
})  
promptAction.showActionMenu({  
  title: '选择分享方式',  
  buttons: buttons  
}).then(data => {  
  if (data.index == 0) {  
  
  } else if (data.index == 1) {  
  
  }  
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
HarmonyOS
2024-09-26 13:16:43
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以参考以下demo。

class UserButton implements promptAction.Button {  
  text: string | Resource;  
  color: string | Resource;  
  
  constructor(text: string | Resource, color?: string | Resource) {  
    this.text = text  
    this.color = color ?? '#000000'  
  }  
}  
  
function createDia(buttonStr: string[]) {  
  let buttons: [  
    promptAction.Button,  
    promptAction.Button?,  
    promptAction.Button?,  
    promptAction.Button?,  
    promptAction.Button?,  
    promptAction.Button?  
  ] = [new UserButton(buttonStr[0])]  
  let len = buttonStr.length > buttons.length ? buttons.length : buttonStr.length  
  for (let i = 1; i < len; i++) {  
    buttons[i] = new UserButton(buttonStr[i])  
  }  
  if (len) {  
    try {  
      promptAction.showActionMenu({  
        title: 'Title Info',  
        buttons: buttons,  
      })  
        .then(data => {  
          console.info('showDialog success, click button: ' + data.index);  
        })  
        .catch((err: Error) => {  
          console.info('showDialog error: ' + err);  
        })  
    } catch (error) {  
      console.error(`showDialog args error code is ${error.code}, message is ${error.message}`);  
    }  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-09-26 18:13:05
相关问题
HarmonyOS Checkbox如何动态
659浏览 • 1回复 待解决
HarmonyOS 页面反向怎么
861浏览 • 1回复 待解决
HarmonyOS 页面
847浏览 • 1回复 待解决
HarmonyOS 如何实现页面反向
1129浏览 • 1回复 待解决
HarmonyOS原生如何给flutter
1093浏览 • 1回复 待解决
HarmonyOS 父子组件问题
758浏览 • 1回复 待解决
HarmonyOS 界面逆向
1148浏览 • 1回复 待解决
HarmonyOS Component问题
627浏览 • 2回复 待解决
如何动态设置组件属性
1445浏览 • 1回复 待解决
ArkTS如何进行页面间
2428浏览 • 1回复 待解决
HarmonyOS AKI是否支持引用
711浏览 • 1回复 待解决
HarmonyOS navigation页面之间回
828浏览 • 1回复 待解决
HarmonyOS关于AXIOS动态参数问题
1442浏览 • 1回复 待解决
在ArkTS中如何进行页面之间
740浏览 • 0回复 待解决
ArkTS自定义组件如何父子间
1396浏览 • 1回复 待解决
自定义组件和绑定
1691浏览 • 1回复 待解决
ArkTS 网络请求 接口动态
1316浏览 • 1回复 待解决
OpenHarmony 使用WEB组件问题
4552浏览 • 1回复 待解决