createAsyncComponent.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. defineAsyncComponent,
  3. // FunctionalComponent, CSSProperties
  4. } from 'vue';
  5. import { Spin } from 'ant-design-vue';
  6. import { noop } from '/@/utils';
  7. // const Loading: FunctionalComponent<{ size: 'small' | 'default' | 'large' }> = (props) => {
  8. // const style: CSSProperties = {
  9. // position: 'absolute',
  10. // display: 'flex',
  11. // justifyContent: 'center',
  12. // alignItems: 'center',
  13. // };
  14. // return (
  15. // <div style={style}>
  16. // <Spin spinning={true} size={props.size} />
  17. // </div>
  18. // );
  19. // };
  20. interface Options {
  21. size?: 'default' | 'small' | 'large';
  22. delay?: number;
  23. timeout?: number;
  24. loading?: boolean;
  25. retry?: boolean;
  26. }
  27. export function createAsyncComponent(loader: Fn, options: Options = {}) {
  28. const { size = 'small', delay = 100, timeout = 30000, loading = false, retry = true } = options;
  29. return defineAsyncComponent({
  30. loader,
  31. loadingComponent: loading ? <Spin spinning={true} size={size} /> : undefined,
  32. // The error component will be displayed if a timeout is
  33. // provided and exceeded. Default: Infinity.
  34. // TODO
  35. timeout,
  36. // errorComponent
  37. // Defining if component is suspensible. Default: true.
  38. // suspensible: false,
  39. delay,
  40. /**
  41. *
  42. * @param {*} error Error message object
  43. * @param {*} retry A function that indicating whether the async component should retry when the loader promise rejects
  44. * @param {*} fail End of failure
  45. * @param {*} attempts Maximum allowed retries number
  46. */
  47. onError: !retry
  48. ? noop
  49. : (error, retry, fail, attempts) => {
  50. if (error.message.match(/fetch/) && attempts <= 3) {
  51. // retry on fetch errors, 3 max attempts
  52. retry();
  53. } else {
  54. // Note that retry/fail are like resolve/reject of a promise:
  55. // one of them must be called for the error handling to continue.
  56. fail();
  57. }
  58. },
  59. });
  60. }