@capacitor/dialog
Dialog API 提供了多种方法来触发原生对话框,可用于显示警告提示、确认窗口和输入框。
安装
npm install @capacitor/dialog@latest-5
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>
显示确认对话框
参数 | Type |
---|---|
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.cover0 |
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 |