Ben Lin
2025-03-08 72675157c774fc4c6490bd1e49ca3c4aa25581f0
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { defineComponent } from 'vue';
import { Card, Typography, Button, Space, message } from 'ant-design-vue';
import { imitateApi } from './mock-api';
import { useRequest } from '@vben/hooks';
import { PageWrapper } from '@/components/Page';
 
const Demo1 = defineComponent({
  setup() {
    const { data, loading, run, cancel } = useRequest(imitateApi, {
      pollingInterval: 1000,
      pollingWhenHidden: false,
      // onSuccess() {
      //   console.log('不可见是否运行呢'); // 测试不可见时,是否还在执行
      // },
    });
 
    return () => (
      <Card title="默认用法">
        <Typography>
          <Typography.Paragraph>
            通过设置
            <Typography.Text type="danger"> options.pollingInterval </Typography.Text>
            ,进入轮询模式,useRequest 会定时触发 service 执行。
          </Typography.Paragraph>
          <Typography.Paragraph>
            <Typography.Text code>
              {`const { data, run, cancel } = useRequest(imitateApi, { pollingInterval: 3000 });`}
            </Typography.Text>
          </Typography.Paragraph>
        </Typography>
 
        <div>
          <div>Username: {loading.value ? 'Loading' : data.value}</div>
          <Space>
            <Button onClick={() => run()}>start</Button>
            <Button type="dashed" onClick={cancel}>
              stop
            </Button>
          </Space>
        </div>
      </Card>
    );
  },
});
 
const Demo2 = defineComponent({
  setup() {
    const { data, loading, run, cancel } = useRequest(imitateApi, {
      manual: true,
      pollingInterval: 3000,
      pollingErrorRetryCount: 3,
      pollingWhenHidden: false,
      onError: (error) => {
        message.error(error.message);
      },
    });
 
    return () => (
      <Card title="轮询错误重试" class="mt-2">
        <Typography>
          <Typography.Paragraph>
            通过
            <Typography.Text type="danger"> options.pollingErrorRetryCount </Typography.Text>
            轮询错误重试次数。
          </Typography.Paragraph>
          <Typography.Paragraph>
            <Typography.Text code>
              {`const { data, run, cancel } = useRequest(imitateApi, { pollingInterval: 3000,  pollingErrorRetryCount: 3 });`}
            </Typography.Text>
          </Typography.Paragraph>
        </Typography>
 
        <div>
          <div>Username: {loading.value ? 'Loading' : data.value}</div>
          <Space>
            <Button onClick={() => run('lutz', false)}>start</Button>
            <Button type="dashed" onClick={cancel}>
              stop
            </Button>
          </Space>
        </div>
      </Card>
    );
  },
});
 
export default defineComponent({
  setup() {
    return () => (
      <PageWrapper>
        <Demo1 />
        <Demo2 />
      </PageWrapper>
    );
  },
});