本文共 3141 字,大约阅读时间需要 10 分钟。
此外,Element-Plus支持国际化功能,通过自定义钩子useI18n实现语言切换。
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus' import { useI18n } from './useI18n' export const useMessage = () => { const { t } = useI18n() return { info(content: string) { ElMessage.info(content) }, error(content: string) { ElMessage.error(content) }, success(content: string) { ElMessage.success(content) }, warning(content: string) { ElMessage.warning(content) }, alert(content: string) { ElMessageBox.alert(content, t('common.confirmTitle')) }, alertError(content: string) { ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'error' }) }, alertSuccess(content: string) { ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'success' }) }, alertWarning(content: string) { ElMessageBox.alert(content, t('common.confirmTitle'), { type: 'warning' }) }, notify(content: string) { ElNotification.info(content) }, notifyError(content: string) { ElNotification.error(content) }, notifySuccess(content: string) { ElNotification.success(content) }, notifyWarning(content: string) { ElNotification.warning(content) }, confirm(content: string, tip?: string) { return ElMessageBox.confirm(content, tip ? tip : t('common.confirmTitle'), { confirmButtonText: t('common.ok'), cancelButtonText: t('common.cancel'), type: 'warning' }) }, delConfirm(content?: string, tip?: string) { return ElMessageBox.confirm( content ? content : t('common.delMessage'), tip ? tip : t('common.confirmTitle'), { confirmButtonText: t('common.ok'), cancelButtonText: t('common.cancel'), type: 'warning' } ) }, exportConfirm(content?: string, tip?: string) { return ElMessageBox.confirm( content ? content : t('common.exportMessage'), tip ? tip : t('common.confirmTitle'), { confirmButtonText: t('common.ok'), cancelButtonText: t('common.cancel'), type: 'warning' } ) }, prompt(content: string, tip: string) { return ElMessageBox.prompt(content, tip, { confirmButtonText: t('common.ok'), cancelButtonText: t('common.cancel'), type: 'warning' }) } } } 以上内容通过模块化设计,便于扩展和维护。每个函数都配备清晰的注释,支持国际化翻译和多种用户交互场景。
转载地址:http://casfk.baihongyu.com/