useToast.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import 'vant/es/notify/style';
  2. import { Toast, showLoadingToast, showSuccessToast, showFailToast, closeToast } from "vant";
  3. /**
  4. * 文字:Toast 提示组件
  5. * @param {String} text
  6. * @param {Number} duration
  7. */
  8. const toastText = (text = '', duration = 2000) => {
  9. showToast({
  10. message: text,
  11. duration: duration,
  12. forbidClick: true
  13. });
  14. }
  15. /**
  16. * 加载:Toast 提示组件
  17. * @param {String} text
  18. * @param {Number} duration
  19. */
  20. const toastLoading = (text = '', duration = 2000) => {
  21. showLoadingToast({
  22. message: text,
  23. duration: duration,
  24. forbidClick: true
  25. });
  26. }
  27. /**
  28. * 成功过:Toast 提示组件
  29. * @param {String} text
  30. * @param {Number} duration
  31. */
  32. const toastSuccess = (text = '', duration = 2000) => {
  33. showSuccessToast({
  34. message: text,
  35. duration: duration,
  36. forbidClick: true
  37. });
  38. }
  39. /**
  40. * 失败:Toast 提示组件
  41. * @param {String} text
  42. * @param {Number} duration
  43. */
  44. const toastFail = (text = '', duration = 2000) => {
  45. showFailToast({
  46. message: text,
  47. duration: duration,
  48. forbidClick: true
  49. });
  50. }
  51. /**
  52. * 清除所有的 Toast 提示
  53. */
  54. const clearToast = () => {
  55. closeToast(false)
  56. }
  57. const useShowToast = () => {
  58. return {
  59. toastText,
  60. toastLoading,
  61. toastSuccess,
  62. toastFail,
  63. clearToast
  64. }
  65. }
  66. export default useShowToast;