@capacitor/dialog
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 | AlertOptions |
起始版本 1.0.0
prompt(...)
prompt(options: PromptOptions) => Promise<PromptResult>
顯示提示對話框
參數 | 類型 |
---|---|
options | PromptOptions |
返回: Promise<PromptResult>
起始版本 1.0.0
confirm(...)
confirm(options: ConfirmOptions) => Promise<ConfirmResult>
顯示確認對話框
參數 | 類型 |
---|---|
options | ConfirmOptions |
返回: Promise<ConfirmResult>
起始版本 1.0.0
介面
AlertOptions
屬性 | 類型 | 描述 | 預設值 | 起始版本 |
---|---|---|---|---|
title | 字串 | 對話框的標題。 | 1.0.0 | |
message | 字串 | 要在對話框上顯示的訊息。 | 1.0.0 | |
buttonTitle | 字串 | 要在動作按鈕上使用的文字。 | "確定" | 1.0.0 |
PromptResult
屬性 | 類型 | 描述 | 起始版本 |
---|---|---|---|
value | 字串 | 在提示中輸入的文字。 | 1.0.0 |
cancelled | 布林值 | 提示是否已取消或接受。 | 1.0.0 |
PromptOptions
屬性 | 類型 | 描述 | 預設值 | 起始版本 |
---|---|---|---|---|
title | 字串 | 對話框的標題。 | 1.0.0 | |
message | 字串 | 要在對話框上顯示的訊息。 | 1.0.0 | |
okButtonTitle | 字串 | 要在肯定動作按鈕上使用的文字。 | "確定" | 1.0.0 |
cancelButtonTitle | 字串 | 要在否定動作按鈕上使用的文字。 | "取消" | 1.0.0 |
inputPlaceholder | 字串 | 提示的預留位置文字。 | 1.0.0 | |
inputText | 字串 | 預先填入的文字。 | 1.0.0 |
ConfirmResult
屬性 | 類型 | 描述 | 起始版本 |
---|---|---|---|
value | 布林值 | 如果點擊了肯定按鈕,則為 true,否則為 false。 | 1.0.0 |
ConfirmOptions
屬性 | 類型 | 描述 | 預設值 | 起始版本 |
---|---|---|---|---|
title | 字串 | 對話框的標題。 | 1.0.0 | |
message | 字串 | 要在對話框上顯示的訊息。 | 1.0.0 | |
okButtonTitle | 字串 | 要在肯定動作按鈕上使用的文字。 | "確定" | 1.0.0 |
cancelButtonTitle | 字串 | 要在否定動作按鈕上使用的文字。 | "取消" | 1.0.0 |