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