@capacitor/dialog
对话框 API 提供触发原生对话框窗口的方法,用于警报、确认和输入提示
安装
npm install @capacitor/dialog
npx cap sync
示例
import { Dialog } from '@capacitor/dialog';
const showAlert = async () => {
await Dialog.alert({
title: 'Stop',
message: 'this is an error',
});
};
const showConfirm = async () => {
const { value } = await Dialog.confirm({
title: 'Confirm',
message: `Are you sure you'd like to press the red button?`,
});
console.log('Confirmed:', value);
};
const showPrompt = async () => {
const { value, cancelled } = await Dialog.prompt({
title: 'Hello',
message: `What's your name?`,
});
console.log('Name:', value);
console.log('Cancelled:', 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
Interfaces
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 |