toast.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * 全局 Toast 工具方法
  3. * 通过 uni 全局事件与 ay-toast 组件通信
  4. *
  5. * 使用方式(在非组件模块中,如 HTTP 请求):
  6. * import toast from '@/utils/toast.js'
  7. * toast.success('操作成功')
  8. * toast.error('操作失败')
  9. * toast.loading('加载中...')
  10. * toast.hide()
  11. */
  12. const toast = {
  13. /**
  14. * 显示成功提示
  15. * @param {String} title - 提示文字
  16. * @param {Object} options - 额外配置
  17. */
  18. success(title = '操作成功', options = {}) {
  19. uni.$emit('ayToast:show', {
  20. type: 'success',
  21. title,
  22. duration: 2000,
  23. ...options
  24. })
  25. },
  26. /**
  27. * 显示错误提示
  28. * @param {String} title - 提示文字
  29. * @param {Object} options - 额外配置
  30. */
  31. error(title = '操作失败', options = {}) {
  32. uni.$emit('ayToast:show', {
  33. type: 'error',
  34. title,
  35. duration: 2500,
  36. ...options
  37. })
  38. },
  39. /**
  40. * 显示加载提示
  41. * @param {String} title - 提示文字
  42. * @param {Object} options - 额外配置
  43. */
  44. loading(title = '加载中...', options = {}) {
  45. uni.$emit('ayToast:show', {
  46. type: 'loading',
  47. title,
  48. duration: 0,
  49. mask: true,
  50. ...options
  51. })
  52. },
  53. /**
  54. * 显示纯文字提示
  55. * @param {String} title - 提示文字
  56. * @param {Object} options - 额外配置
  57. */
  58. text(title, options = {}) {
  59. uni.$emit('ayToast:show', {
  60. type: 'text',
  61. title,
  62. duration: 2000,
  63. ...options
  64. })
  65. },
  66. /**
  67. * 隐藏 Toast
  68. */
  69. hide() {
  70. uni.$emit('ayToast:hide')
  71. }
  72. }
  73. export default toast