@capacitor/dialog
Dialog API 提供触发原生对话框窗口的方法,用于警告、确认和输入提示。
安装
npm install @capacitor/dialog
npx cap sync
示例
import { Dialog } from '@capacitor/dialog';
const showAlert = async () => {
await Dialog.alert({
title: '停止',
message: '这是一个错误',
});
};
const showConfirm = async () => {
const { value } = await Dialog.confirm({
title: '确认',
message: '您确定要按下红色按钮吗?',
});
console.log('已确认:', value);
};
const showPrompt = async () => {
const { value, cancelled } = await Dialog.prompt({
title: '您好',
message: '您叫什么名字?',
});
console.log('姓名:', value);
console.log('已取消:', cancelled);
};
API
alert(...)
alert(options: AlertOptions) => Promise<void>
显示一个警告对话框。
| 参数 | 类型 |
|---|---|
options | |
起始版本: 1.0.0
prompt(...)
prompt(options: PromptOptions) => Promise<PromptResult>
显示一个输入提示对话框。
| 参数 | 类型 |
|---|---|
options | |
返回:
Promise<PromptResult>
起始版本: 1.0.0
confirm(...)
confirm(options: ConfirmOptions) => Promise<ConfirmResult>
显示一个确认对话框。
| 参数 | 类型 |
|---|---|
options | |
返回:
Promise<ConfirmResult>
起始版本: 1.0.0
接口
AlertOptions
| 属性 | 类型 | 描述 | 默认值 | 起始版本 |
|---|---|---|---|---|
title | string | 对话框的标题。 | 1.0.0 | |
message | string | 在对话框上显示的消息。 | 1.0.0 | |
buttonTitle | string | 操作按钮上使用的文本。 | "OK" | 1.0.0 |
PromptResult
| 属性 | 类型 | 描述 | 起始版本 |
|---|---|---|---|
value | string | 在提示框中输入的文本。 | 1.0.0 |
cancelled | boolean | 提示框是否被取消或接受。 | 1.0.0 |
PromptOptions
| 属性 | 类型 | 描述 | 默认值 | 起始版本 |
|---|---|---|---|---|
title | string | 对话框的标题。 | 1.0.0 | |
message | string | 在对话框上显示的消息。 | 1.0.0 | |
okButtonTitle | string | 肯定操作按钮上使用的文本。 | "OK" | 1.0.0 |
cancelButtonTitle | string | 否定操作按钮上使用的文本。 | "Cancel" | 1.0.0 |
inputPlaceholder | string | 提示的占位文本。 | 1.0.0 | |
inputText | string | 预填文本。 | 1.0.0 |
ConfirmResult
| 属性 | 类型 | 描述 | 起始版本 |
|---|---|---|---|
value | boolean | 点击了肯定按钮则返回 true,否则返回 false。 | 1.0.0 |
ConfirmOptions
| 属性 | 类型 | 描述 | 默认值 | 起始版本 |
|---|---|---|---|---|
title | string | 对话框的标题。 | 1.0.0 | |
message | string | 在对话框上显示的消息。 | 1.0.0 | |
okButtonTitle | string | 肯定操作按钮上使用的文本。 | "OK" | 1.0.0 |
cancelButtonTitle | string | 否定操作按钮上使用的文本。 | "Cancel" | 1.0.0 |