HarmonyOS鸿蒙Next中prompAction.showToast

HarmonyOS鸿蒙Next中prompAction.showToast prompAction.showToast这个弹窗也废弃了吗?那现在用什么弹窗?

5 回复

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-uicontext-promptaction#showtoast

import { PromptAction } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  promptAction: PromptAction = this.getUIContext().getPromptAction();

  build() {
    Column() {
      Button('showToast')
        .onClick(() => {
          try {
            this.promptAction.showToast({
              message: 'Message Info',
              duration: 2000
            });
          } catch (error) {
            let message = (error as BusinessError).message;
            let code = (error as BusinessError).code;
            console.error(`showToast args error code is ${code}, message is ${message}`);
          };
        })
    }.height('100%').width('100%').justifyContent(FlexAlign.Center)
  }
}

官方没事就就会废弃一个弹框,这才几个月啊已经废弃了无数的弹窗了。。。

在HarmonyOS Next中,promptAction.showToast用于显示短暂的消息提示。该API属于UI组件,通过指定消息内容和显示时长实现。调用方式为promptAction.showToast({ message: ‘文本’, duration: 时长 })。duration参数可选,单位为毫秒,默认值为1500。该提示会自动消失,不干扰用户操作。

是的,promptAction.showToast已在HarmonyOS Next中废弃,建议使用新的UI组件Toast进行替代。具体可通过@ohos.promptAction模块中的Toast类实现,例如:

import promptAction from '@ohos.promptAction';

let toast = promptAction.showToast({
  message: '提示信息',
  duration: 2000
});

该方式符合HarmonyOS Next的API规范,推荐在开发中使用。

回到顶部