HarmonyOS APP

Modal 弹窗 composable,提供简洁的模态框显示功能,支持全局/局部模式和函数式调用。
useModal 是一个函数式调用 Modal 的解决方案:
show 和 confirm 两种显示方式<template>
<u-modal global />
</template>import { useModal } from 'uview-pro';
const modal = useModal();
modal.show('全局提示');useModal 支持以下调用方式:
// 方式1:默认全局模式
const modal = useModal();
// 方式2:指定全局/局部模式
const modal = useModal({ global: true });
const modal = useModal({ page: true });
// 方式3:简写形式
const modal = useModal(true); // 全局
const modal = useModal(false); // 局部返回一个包含以下方法的对象:
| 方法 | 参数 | 说明 |
|---|---|---|
show(contentOrOptions) | string | ModalPayload | 显示 Modal(单按钮) |
confirm(contentOrOptions) | string | ModalPayload | 显示确认 Modal(双按钮) |
close() | - | 关闭 Modal |
clearLoading() | - | 清空 loading 状态 |
type ModalPayload = Partial<Omit<ModalProps, 'modelValue'>> & {
/** 标题 */
title?: string;
/** 内容 */
content?: string;
/** 确认回调 */
onConfirm?: () => void;
/** 取消回调 */
onCancel?: () => void;
};type ModalProps = {
/** 层级z-index */
zIndex?: number | string;
/** 弹窗宽度 */
width?: number | string;
/** 是否显示标题 */
showTitle?: boolean;
/** 是否显示确认按钮 */
showConfirmButton?: boolean;
/** 是否显示取消按钮 */
showCancelButton?: boolean;
/** 确认文案 */
confirmText?: string;
/** 取消文案 */
cancelText?: string;
/** 确认按钮颜色 */
confirmColor?: string;
/** 取消文字颜色 */
cancelColor?: string;
/** 圆角值 */
borderRadius?: number | string;
/** 是否开启缩放效果 */
zoom?: boolean;
/** 是否异步关闭 */
asyncClose?: boolean;
/** 是否允许点击遮罩关闭 */
maskCloseAble?: boolean;
};<script setup lang="ts">
import { useModal } from 'uview-pro';
// 默认全局模式
const modal = useModal();
// 显示简单弹窗
modal.show('操作提示');
</script><script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
// 方式1:直接传入字符串(作为内容)
modal.confirm('确定要删除这条数据吗?');
// 方式2:传入对象
modal.confirm({
title: '确认删除',
content: '删除后数据将无法恢复,请确认是否继续?',
onConfirm: () => {
console.log('用户点击了确认');
// 执行删除操作
},
onCancel: () => {
console.log('用户点击了取消');
}
});
</script><script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
modal.confirm({
title: '温馨提示',
content: '您确定要退出登录吗?',
confirmText: '退出',
cancelText: '取消',
onConfirm: () => {
// 退出登录
uni.removeStorageSync('token');
uni.reLaunch({ url: '/pages/login/index' });
}
});<script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
modal.show({
title: '操作成功',
content: '您的订单已提交成功,请在“我的订单”中查看详情。'
});<script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
modal.confirm({
title: '自定义弹窗',
content: '这是一个自定义宽度和样式的弹窗',
width: 500,
confirmColor: '#ff4d4f',
cancelColor: '#999',
borderRadius: 8,
onConfirm: () => {
console.log('确认操作');
}
});<script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
modal.confirm({
title: '提交表单',
content: '确定要提交表单吗?',
asyncClose: true, // 启用异步关闭
onConfirm: async () => {
// 模拟网络请求
await submitForm();
// 请求完成后手动关闭
modal.close();
},
onCancel: () => {
modal.close();
}
});<script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
// 显示弹窗
modal.show({
title: '提示',
content: '3秒后自动关闭',
maskCloseAble: true // 允许点击遮罩关闭
});
// 手动关闭
setTimeout(() => {
modal.close();
}, 3000);全局模式使用根目录下的 <u-modal global />,可在任何页面调用。
<script setup lang="ts">
import { useModal } from 'uview-pro';
// 全局模式(默认)
const modal = useModal();
// 或
const modal = useModal({ global: true });
// 或
const modal = useToast(true);
modal.show('全局弹窗提示'); // 整个应用任意位置都能显示注意: 使用全局模式时,需要在 App.vue 中添加全局 Modal 组件:
<!-- App.vue -->
<template>
<u-modal global />
</template>局部模式使用当前页面的 <u-modal page />,不影响其他页面。
<script setup lang="ts">
import { useModal } from 'uview-pro';
// 局部模式
const modal = useModal(false);
// 或
const modal = useModal({ page: true });
modal.show('页面内弹窗'); // 只在当前页面显示注意: 使用局部模式时,需要在当前页面添加 <u-modal /> 组件。
<template>
<view>
<!-- 其他内容 -->
<u-modal page />
</view>
</template><script setup lang="ts">
import { useToast } from 'uview-pro';
const modal = useModal();
const toast = useToast();
const handleDelete = (item: any) => {
modal.confirm({
title: '确认删除',
content: `确定要删除「${item.name}」吗?`,
confirmColor: '#ff4d4f',
onConfirm: async () => {
try {
await deleteItem(item.id);
toast.success('删除成功');
} catch (error) {
toast.error('删除失败');
}
},
onCancel: () => {
console.log('取消删除');
}
});
};
</script><script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
const showLoginExpired = () => {
modal.confirm({
title: '登录已过期',
content: '您的登录状态已过期,请重新登录。',
confirmText: '重新登录',
cancelText: '暂不登录',
onConfirm: () => {
uni.removeStorageSync('token');
uni.reLaunch({ url: '/pages/login/index' });
},
onCancel: () => {
uni.switchTab({ url: '/pages/home/index' });
}
});
};<script setup lang="ts">
import { useModal } from 'uview-pro';
const modal = useModal();
const checkVersionUpdate = (versionInfo: any) => {
if (versionInfo.forceUpdate) {
// 强制更新
modal.confirm({
title: '版本更新',
content: `发现新版本 v${versionInfo.version},请立即更新以获得更好的体验。`,
showCancelButton: false, // 隐藏取消按钮
confirmText: '立即更新',
onConfirm: () => {
// 跳转应用商店
plus.runtime.openURL(versionInfo.downloadUrl);
}
});
} else {
// 可选更新
modal.confirm({
title: '版本更新',
content: `发现新版本 v${versionInfo.version},是否更新?`,
onConfirm: () => {
plus.runtime.openURL(versionInfo.downloadUrl);
}
});
}
};// utils/confirm.ts
import { useModal } from 'uview-pro';
const modal = useModal();
interface ConfirmOptions {
title?: string;
content: string;
confirmText?: string;
cancelText?: string;
confirmColor?: string;
onConfirm?: () => void;
onCancel?: () => void;
}
export const confirm = (options: ConfirmOptions) => {
const { title = '确认提示', content, confirmText = '确认', cancelText = '取消', confirmColor, onConfirm, onCancel } = options;
return modal.confirm({
title,
content,
confirmText,
cancelText,
confirmColor,
onConfirm,
onCancel
});
};
export const alert = (content: string, title = '提示') => {
return modal.show({ title, content });
};// utils/deleteConfirm.ts
import { useModal } from 'uview-pro';
const modal = useModal();
export const deleteConfirm = (itemName: string, onConfirm: () => void) => {
return modal.confirm({
title: '确认删除',
content: `确定要删除「${itemName}」吗?`,
confirmColor: '#ff4d4f',
onConfirm
});
};全局模式 vs 局部模式:
<u-modal global />,通常放在 App.vue 中<u-modal page /><u-modal global /> 或 <u-modal page /> 实例用于承接事件show vs confirm:
show:显示单按钮弹窗,只有确认按钮confirm:显示双按钮弹窗,包含确认和取消按钮异步关闭:
asyncClose: true 时,需要在 onConfirm 回调中手动调用 modal.close() 关闭弹窗事件回调:
onConfirm:用户点击确认按钮时触发onCancel:用户点击取消按钮时触发参数兼容:
content| 特性 | useModal | u-modal 组件 |
|---|---|---|
| 调用方式 | 函数式调用 | ref 引用调用 |
| 适用场景 | 快速弹窗提示 | 复杂交互/自定义 |
| 全局支持 | ✅ | ✅ |
| 局部支持 | ✅ | ✅ |
| 学习成本 | 低 | 中 |
