鸿蒙NEXT开发:ArkUI框架UI界面-@ohos.promptAction (弹窗)

 往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)


@ohos.promptAction (弹窗)

创建并显示文本提示框、对话框和操作菜单。

说明

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

该模块不支持在UIAbility的文件声明处使用,即不能在UIAbility的生命周期中调用,需要在创建组件实例后使用。

本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见UIContext说明。建议在无UI界面的场景外,均使用UIContext中的弹窗方法。

从API version 10开始,可以通过使用UIContext中的getPromptAction方法获取当前UI上下文关联的PromptAction对象。

导入模块

import { promptAction } from '@kit.ArkUI';

promptAction.showToast

showToast(options: ShowToastOptions): void

创建并显示文本提示框。·

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
options ShowToastOptions 文本弹窗选项。

错误码:

以下错误码的详细介绍。

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.
100001 Internal error.

示例:

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

@Entry
@Component
struct toastExample {
  build() {
    Column() {
      Button('Show toast').fontSize(20)
        .onClick(() => {
          try {
            promptAction.showToast({
              message: 'Hello World',
              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)
  }
}

API version 11及之前Toast样式。

API version 12及之后Toast样式。

promptAction.showDialog

showDialog(options: ShowDialogOptions): Promise<ShowDialogSuccessResponse>

创建并显示对话框,对话框响应后异步返回结果。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
options ShowDialogOptions 对话框选项。

返回值:

类型 说明
Promise<ShowDialogSuccessResponse> 对话框响应结果。

错误码:

以下错误码的详细介绍。

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.
100001 Internal error.

示例:

import { promptAction } from '@kit.ArkUI'

promptAction.showDialog({
  title: 'Title Info',
  message: 'Message Info',
  buttons: [
    {
      text: 'button1',
      color: '#000000'
    },
    {
      text: 'button2',
      color: '#000000'
    }
  ],
})
  .then(data => {
    console.info('showDialog success, click button: ' + data.index);
  })
  .catch((err: Error) => {
    console.info('showDialog error: ' + err);
  })

promptAction.showDialog

showDialog(options: ShowDialogOptions, callback: AsyncCallback<ShowDialogSuccessResponse>):void

创建并显示对话框,对话框响应结果异步返回。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名 类型 必填 说明
options ShowDialogOptions 页面显示对话框信息描述。
callback AsyncCallback<ShowDialogSuccessResponse> 对话框响应结果回调。

错误码:

以下错误码的详细介绍。

错误码ID 错误信息
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed.
100001 Internal error.

示例:

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

try {
  promptAction.showDialog({
    title: 'showDialog Title Info',
    message: 'Message Info',
    buttons: [
      {
        text: 'button1',
        color: '#000000'
      },
      {
        text: 'button2',
        color: '#000000'
      }
    ]
  }, (err, data) => {
    if (err) {
      console.info('showDialog err: ' + err);
      return;
    }
    console.info('showDialog success callback, click button: ' + data.index);
  });
} catch (error) {
  let message = (error as BusinessError).message
  let code = (error as BusinessError).code
  console.error(`showDialog args error code is ${code}, message is ${message}`);
};

当弹窗的showInSubWindow属性为true时,弹窗可显示在窗口外。

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

try {
  promptAction.showDialog({
    title: 'showDialog Title Info',
    message: 'Message Info',
    isModal: true,
    showInSubWindow: true,
    buttons: [
      {
        text: 'button1',
        color: '#000000'
      },
      {
        text: 'button2',
        color: '#000000'
      }
    ]
  }, (err, data) => {
    if (err) {
      console.info('showDialog err: ' + err);
      return;
    }
    console.info('showDialog success callback, click button: ' + data.index);
  });
} catch (error) {
  let message = (error as BusinessError).message
  let code = (error as BusinessError).code
  console.error(`showDialog args error code is ${code}, message is ${message}`);
};

promptAction.showActionMenu

showActionMenu(options: ActionMenuOptions, callback: AsyncCallback<ActionMenuSuccessResponse>):void

创建并显示操作菜单,菜单响应结果异步返回。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full。

参数:

参数名 类型 必填 说明
options
<think>我们正在处理一个关于ArkTS的模块导入错误。用户遇到了"Cannot findmodule '@ohos.arkui.advanced'or itstype declarations"的错误。这个错误通常发生在TypeScript(或ArkTS)环境中,当编译器无法找到指定的模块或它的类型声明文件时。根据错误信息,我们可以推测:1.用户可能没有正确安装或配置该模块。2.模块可能不存在于用户的项目依赖中。3.类型声明文件(.d.ts)可能缺失或配置不正确。在HarmonyOS开发中,'@ohos'开头的模块通常是系统提供的模块。但是,'@ohos.arkui.advanced'这个模块名称看起来不太常见(通常我们可能使用'@ohos.arkui'等)。因此,我们首先需要确认这个模块是否存在。 查阅HarmonyOS官方文档,目前公开的ArkUI模块主要是'@ohos.arkui'(提供基础能力)和一些其他模块(如'@ohos.arkui.ability'等)。而'@ohos.arkui.advanced'这个模块名称在公开文档中并不常见。因此,我们怀疑这个模块名称可能是用户自定义的,或者是内部模块?但根据错误提示,我们更倾向于认为这个模块名称可能写错了。但是,考虑到用户可能是在参考某个示例或文档,我们需要进一步确认:步骤1:检查模块名称拼写是否正确步骤2:确认该模块是否在HarmonyOS的API版本中存在然而,根据公开的HarmonyOSAPI文档(截至HarmonyOS3.0),并没有找到名为'@ohos.arkui.advanced'的模块。常见的与ArkUI相关的模块有:- @ohos.arkui.ui- @ohos.arkui.ability因此,我们建议用户检查模块名称是否正确。如果用户确实需要使用高级ArkUI功能,可以查阅官方文档以确定正确的模块名称。另外,如果这个模块是用户自定义的,那么需要确保模块已经正确安装并配置了类型声明。解决方案: 1.确认模块名称:检查拼写,并查阅官方文档确认。2.如果模块名称正确,确保已安装相应的依赖(在HarmonyOS中,系统模块不需要安装,但需要确认API版本是否支持)。 3.如果模块是自定义的,确保已经正确构建并生成了类型声明文件,并且在tsconfig.json中配置了路径映射。 对于系统模块,通常不需要安装,但需要确保在项目的build-profile.json5中配置了正确的依赖。例如,在HarmonyOS项目中,我们可以在模块的build-profile.json5中添加依赖:```json"dependencies": [{"bundleName":"ohos.arkui.advanced", //注意:这里应该是实际的bundle名称,但通常我们使用npm模块格式"packageName":"ohos.arkui.advanced",//同样,需要确认实际的包名"versionCode":1 //版本号}] ```但是,由于我们无法确认这个模块的存在,所以更可能的是模块名称错误。 另一种可能是,用户想要使用的高级功能实际上包含在'@ohos.arkui'模块中,因此可以尝试导入'@ohos.arkui'。 如果用户需要的是自定义组件或高级功能,可能是通过扩展的方式提供的,那么需要检查是否已经正确引入了包含这些组件的库。 另外,关于类型声明:HarmonyOSArkUI模块通常都提供了类型声明,但如果用户的项目配置有问题,也可能导致找不到类型声明。可以尝试在tsconfig.json中配置路径映射(如果模块是自定义的)。例如,在tsconfig.json中添加:```json{"compilerOptions":{"baseUrl":".","paths":{"@ohos.arkui.advanced": ["./src/types/arkui-advanced"]//指向类型声明文件所在的目录}} }``` 但是,对于系统模块,不需要这样配置,因为SDK已经提供了类型声明。综上所述,我们建议:1.确认模块名称是否正确,参考官方文档或示例。2.如果模块名称正确,检查HarmonyOSSDK的版本是否支持该模块(可能需要升级SDK)。 3.如果模块是自定义的,确保已经正确导出类型声明,并在tsconfig.json中配置了路径映射。 如果用户仍然无法解决,可以提供更多上下文信息,例如使用的HarmonyOSSDK版本、开发环境以及尝试导入该模块的代码片段。 相关问题: 1.如何正确导入ArkUI模块? 2.HarmonyOS中如何添加模块依赖?3.如何为自定义模块生成类型声明文件?4.如何配置tsconfig.json以解决模块路径问题?</think>### 解决 ArkTS 模块导入错误:`Cannot find module '@ohos.arkui.advanced'` 在 HarmonyOS 开发中,此错误通常由以下原因导致: #### 1. **模块名称错误或未发布** - `@ohos.arkui.advanced` 不是官方公开模块(截至 HarmonyOS 4.0)。 - 官方 ArkUI 模块为 `@ohos.arkui`(基础能力)和 `@ohos.arkui.ability`(扩展能力)[^1]。 - **解决方案**: - 检查模块名称拼写:确认是否应为 `@ohos.arkui.ability` - 查阅[官方文档](https://developer.harmonyos.com/cn/docs/documentation/doc-references/ts-components-0000001333641209)验证可用模块 #### 2. **SDK 版本不兼容** - 旧版 SDK 可能未包含新模块。 - **解决方案**: ```bash # 更新HarmonyOS SDK sdkmanager --update ``` - 在 `build-profile.json5` 中确认 API 版本: ```json "apiType": "stageModel", // 确保使用Stage模型 "buildOS": "HarmonyOS NEXT" // 检查是否支持新特性 ``` #### 3. **依赖未正确声明** - 模块需在 `module.json5` 显式声明: ```json "dependencies": [ { "bundleName": "ohos.arkui.ability", // 官方模块名 "version": ">1.0.0" // 版本范围 } ] ``` #### 4. **类型声明缺失** - 若使用 TypeScript 开发: ```bash # 安装ArkUI类型声明 npm install @types/harmonyos-arkui --save-dev ``` - 在 `tsconfig.json` 中添加路径映射: ```json { "compilerOptions": { "baseUrl": "./", "paths": { "@ohos/*": ["node_modules/@types/harmonyos-arkui/*"] } } } ``` #### 5. **工程配置问题** - 清理缓存并重新同步: ```bash # 删除缓存 rm -rf .arktscache # 重新构建 ohpm install ``` > **关键提示**: > 当前公开文档中未发现 `@ohos.arkui.advanced` 模块,建议优先验证模块名称正确性。若为自定义模块,需确保已通过 `oh-package.json5` 正确安装并导出类型声明[^2][^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值